179 lines
4.7 KiB
C++
179 lines
4.7 KiB
C++
#include "Game.hpp"
|
|
|
|
Game::Game() : configuration(this)
|
|
{
|
|
std::cout << "GLEW " << glewGetString(GLEW_VERSION) << std::endl;
|
|
putenv("SDL_VIDEO_X11_LEGACY_FULLSCREEN=0");
|
|
putenv("SDL_VIDEO_CENTERED=1");
|
|
SDL_version version;
|
|
SDL_GetVersion(&version);
|
|
printf("SDL %d.%d.%d\n", version.major, version.minor, version.patch);
|
|
fprintf(stderr, "stderr test message\n");
|
|
SDL_SetMainReady();
|
|
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
print_gl_attributes();
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
|
|
{
|
|
print_sdl_error("SDL could not initialize");
|
|
flag_to_end();
|
|
}
|
|
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
print_gl_attributes();
|
|
window = SDL_CreateWindow("TARE control", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
|
sw, sh, SDL_WINDOW_OPENGL);
|
|
if (window == NULL)
|
|
{
|
|
print_sdl_error("Could not create window");
|
|
flag_to_end();
|
|
}
|
|
SDL_ShowCursor(0);
|
|
if (TTF_Init() < 0)
|
|
{
|
|
print_sdl_error("Could not initialize SDL ttf");
|
|
flag_to_end();
|
|
}
|
|
else
|
|
{
|
|
printf("initialized SDL ttf %d.%d.%d\n", SDL_TTF_MAJOR_VERSION,
|
|
SDL_TTF_MINOR_VERSION, SDL_TTF_PATCHLEVEL);
|
|
}
|
|
if (Mix_Init(MIX_INIT_FLAC) == 0)
|
|
{
|
|
print_sdl_error("Could not initialize SDL mixer");
|
|
flag_to_end();
|
|
}
|
|
else
|
|
{
|
|
printf("initialized SDL mixer %d.%d.%d\n", SDL_MIXER_MAJOR_VERSION,
|
|
SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL);
|
|
}
|
|
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT,
|
|
MIX_DEFAULT_CHANNELS, 1024) < 0)
|
|
{
|
|
print_sdl_error("Could not set up audio");
|
|
}
|
|
}
|
|
|
|
void Game::print_error(std::string message)
|
|
{
|
|
std::cerr << message << std::endl;
|
|
}
|
|
|
|
void Game::print_sdl_error(std::string message)
|
|
{
|
|
std::cerr << message << " " << SDL_GetError() << std::endl;
|
|
}
|
|
|
|
void Game::print_gl_attributes()
|
|
{
|
|
int major, minor;
|
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
|
std::cout << "GL CONTEXT: " << major << ", " << minor << std::endl;
|
|
}
|
|
|
|
void Game::load_sdl_context()
|
|
{
|
|
if (glcontext != NULL)
|
|
{
|
|
SDL_GL_DeleteContext(glcontext);
|
|
glcontext = NULL;
|
|
}
|
|
if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL)
|
|
{
|
|
print_sdl_error("Could not create renderer");
|
|
flag_to_end();
|
|
}
|
|
is_gl_context = false;
|
|
}
|
|
|
|
void Game::load_gl_context()
|
|
{
|
|
if (renderer != NULL)
|
|
{
|
|
SDL_DestroyRenderer(renderer);
|
|
renderer = NULL;
|
|
}
|
|
// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
|
print_gl_attributes();
|
|
if ((glcontext = SDL_GL_CreateContext(window)) == NULL)
|
|
{
|
|
print_sdl_error("Could not get GL context");
|
|
flag_to_end();
|
|
}
|
|
GLenum error = glewInit();
|
|
if (error != GLEW_OK)
|
|
{
|
|
std::ostringstream message;
|
|
message << "GLEW could not initialize " << glewGetErrorString(error);
|
|
print_error(message.str());
|
|
}
|
|
printf("OpenGL %s, renderer %s, shading language %s\n", glGetString(GL_VERSION),
|
|
glGetString(GL_RENDERER), glGetString(GL_SHADING_LANGUAGE_VERSION));
|
|
is_gl_context = true;
|
|
}
|
|
|
|
void Game::run()
|
|
{
|
|
while (not done)
|
|
{
|
|
ticks = SDL_GetTicks();
|
|
if (ticks - last_frame_timestamp + frame_time_overflow >= frame_length)
|
|
{
|
|
last_frame_length = ticks - last_frame_timestamp;
|
|
frame_time_overflow = last_frame_length + frame_time_overflow - frame_length;
|
|
last_frame_timestamp = ticks;
|
|
update();
|
|
}
|
|
SDL_Delay(15);
|
|
}
|
|
}
|
|
|
|
// void Game::update()
|
|
// {
|
|
// }
|
|
|
|
void Game::flag_to_end()
|
|
{
|
|
done = true;
|
|
}
|
|
|
|
void Game::set_framerate(int fps)
|
|
{
|
|
if (fps < 1)
|
|
{
|
|
fps = 1;
|
|
}
|
|
framerate = fps;
|
|
frame_length = 1000.0 / framerate;
|
|
}
|
|
|
|
void Game::quit()
|
|
{
|
|
if (glcontext != NULL)
|
|
{
|
|
SDL_GL_DeleteContext(glcontext);
|
|
}
|
|
if (renderer != NULL)
|
|
{
|
|
SDL_DestroyRenderer(renderer);
|
|
}
|
|
if (window != NULL)
|
|
{
|
|
SDL_DestroyWindow(window);
|
|
}
|
|
if (TTF_WasInit())
|
|
{
|
|
TTF_Quit();
|
|
}
|
|
Mix_CloseAudio();
|
|
Mix_Quit();
|
|
SDL_Quit();
|
|
}
|