Skip to content

Instantly share code, notes, and snippets.

@MORTAL2000
Last active March 12, 2017 19:13
Show Gist options
  • Save MORTAL2000/c52e06bf52f50bf7203f7fcace6c4b8f to your computer and use it in GitHub Desktop.
Save MORTAL2000/c52e06bf52f50bf7203f7fcace6c4b8f to your computer and use it in GitHub Desktop.
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <string>
#include "Camera.hpp"
static void update_fps_counter(SDL_Window* window, const std::string& title) {
static std::string text_bar;
static Uint32 fps_lasttime = SDL_GetTicks();
static Uint32 fps_current;
static Uint32 fps_frames;
static const Uint32 FPS_INTERVAL = 1;
fps_frames++;
if (fps_lasttime < SDL_GetTicks() - FPS_INTERVAL * 1000)
{
text_bar = title + " | FPS: " + std::to_string(fps_frames) + " | " + std::to_string(1000.0f / fps_frames) + " ms/frame";
SDL_SetWindowTitle(window, text_bar.c_str());
fps_lasttime = SDL_GetTicks();
fps_current = fps_frames;
fps_frames = 0;
}
}
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
//atexit(SDL_Quit);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
// SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 4);
std::string title = "SDL";
int width = 800;
int height = 600;
bool full_screen = false;
SDL_Window* window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);
SDL_GLContext glContext = SDL_GL_CreateContext(window);
//SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
//SDL_GL_SetSwapInterval(1);
Camera camera;
GLenum res = glewInit();
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
}
// -- opengl --
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.5f, 1.0f, 0.0f);
int mouseX=0;
int mouseY=0;
bool running = true;
Uint32 lastTime = SDL_GetTicks();
float total_time = 0.0f;
while (running)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
running = false;
}
if (event.type == SDL_MOUSEMOTION)
{
//SDL_GetMouseState(&mouseX, &mouseY);
//mouseX = event.motion.x;
//mouseY = event.motion.y;
//printf("mouseX: %i , mouseY: %i\n", mouseX, mouseY);
//printf("> mouseX: %i , mouseY: %i\n", event.motion.x, event.motion.y);
}
if (event.type == SDL_KEYDOWN)
{
//int value = event.key.keysym.scancode;
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
}
}
if (event.type == SDL_KEYUP)
{
//int value = event.key.keysym.scancode;
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = false;
break;
case SDLK_SPACE:
full_screen = !full_screen;
SDL_SetWindowFullscreen(window, full_screen ? SDL_WINDOW_FULLSCREEN : NULL);
break;
}
}
if (event.type == SDL_MOUSEBUTTONDOWN)
{
int value = event.button.button;
}
if (event.type == SDL_MOUSEBUTTONUP)
{
int value = event.button.button;
}
}
// -- update --
//SDL_PumpEvents();
Uint32 time = SDL_GetTicks();
float deltaTime = ((float)(time - lastTime)) / 1000;
total_time += deltaTime;
lastTime = time;
camera.move(deltaTime);
update_fps_counter(window, title);
// -- render --
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment