Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // init_sdl.h
- #ifndef INIT_SDL_H
- #define INIT_SDL_H
- /* Include windows.h properly on Windows */
- #if defined(WIN32) || defined(_WINDOWS)
- # define WIN32_LEAN_AND_MEAN
- # define NOMINMAX
- # include <windows.h>
- #endif
- /* SDL */
- #include <SDL.h>
- #include <SDL_opengl.h>
- #ifndef INIT_SDL_MAIN
- # undef main
- #endif
- #endif
- // main.cpp
- #define INIT_SDL_MAIN
- #include "init_sdl.h"
- #include <SDL_events.h>
- #include <iostream>
- static void criticalError(const char* title, const char* text)
- {
- std::cerr << "Critical error: " << title << std::endl << text << std::endl;
- exit(1);
- }
- struct Time
- {
- double t;
- double dt;
- };
- void init()
- {
- }
- void resize(int w, int h)
- {
- glViewport(0, 0, w, h);
- }
- void update(Time time)
- {
- }
- void render(Time time)
- {
- glClearColor(0.3f, 0.6f, 0.9f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
- }
- void mainLoop(SDL_Window* win)
- {
- uint64_t counterOld = SDL_GetPerformanceCounter();
- Time time = { 0.0, 0.0};
- int winWidth;
- int winHeight;
- SDL_GetWindowSize(win, &winWidth, &winHeight);
- init();
- resize(winWidth, winHeight);
- bool running = true;
- while (running)
- {
- uint64_t counterNow = SDL_GetPerformanceCounter();
- uint64_t counterFreq = SDL_GetPerformanceFrequency();
- time.dt = double(counterNow - counterOld) / double(counterFreq);
- if (time.dt > 0.1)
- time.dt = 1.0 / 60.0;
- time.t += time.dt;
- SDL_Event evt;
- while (SDL_PollEvent(&evt))
- {
- switch (evt.type)
- {
- case SDL_QUIT:
- running = false;
- break;
- case SDL_WINDOWEVENT:
- SDL_WindowEvent winevt = evt.window;
- switch (winevt.event)
- {
- case SDL_WINDOWEVENT_RESIZED:
- winWidth = winevt.data1;
- winHeight = winevt.data2;
- resize(winWidth, winHeight);
- break;
- }
- break;
- }
- }
- if (time.dt > 0.0)
- update(time);
- render(time);
- SDL_GL_SwapWindow(win);
- SDL_Delay(1);
- }
- }
- int main(int argc, char** argv)
- {
- SDL_Window* win;
- SDL_GLContext ctx;
- const int winWidth = 800;
- const int winHeight = 600;
- int result = SDL_Init(SDL_INIT_VIDEO);
- if (result < 0)
- criticalError("Could not initialize SDL", SDL_GetError());
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
- SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
- 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_CONTEXT_MAJOR_VERSION, 3);
- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
- win = SDL_CreateWindow("Hello SDL",
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- winWidth, winHeight,
- SDL_WINDOW_OPENGL
- | SDL_WINDOW_SHOWN
- | SDL_WINDOW_RESIZABLE);
- if (!win)
- criticalError("Unable to create render window", SDL_GetError());
- ctx = SDL_GL_CreateContext(win);
- SDL_GL_SetSwapInterval(1);
- mainLoop(win);
- SDL_GL_DeleteContext(ctx);
- SDL_DestroyWindow(win);
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement