Skip to content

Instantly share code, notes, and snippets.

@umbra-scientia
Created June 16, 2020 00:35
Show Gist options
  • Save umbra-scientia/ce289464a7d4c6abfd25f44bf3d67d5d to your computer and use it in GitHub Desktop.
Save umbra-scientia/ce289464a7d4c6abfd25f44bf3d67d5d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <SDL.h>
#include <GL/glew.h>
#include <math.h>
#define BTN_QUIT 256
#define BTN_MOUSE0 257
#define BTN_MOUSE1 258
#define BTN_MOUSE2 259
#define BTN_MOUSE3 260
#define BTN_MOUSE4 261
#define BTN_MOUSE_LEFT 258
#define BTN_MOUSE_RIGHT 260
SDL_Window* win = 0;
SDL_GLContext ctx;
int done = 0;
int grab = 0;
int w=1440, h=810;
int resized = 1;
int omx=0, omy=0;
void inputMotion(float x, float y) {}
void inputButton(int btn, int state) {}
void render() {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT);
}
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
win = SDL_CreateWindow("sdlboilerplate", 0, 0, w, h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
ctx = SDL_GL_CreateContext(win);
glewInit();
SDL_GetMouseState(&omx, &omy);
SDL_GetWindowSize(win, &w, &h);
while (!done) {
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_WINDOWEVENT:
if (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
w = e.window.data1;
h = e.window.data2;
resized = 1;
}
break;
case SDL_QUIT:
if (!grab) done = 1;
inputButton(BTN_QUIT, 1);
break;
case SDL_MOUSEBUTTONDOWN:
inputButton(BTN_MOUSE0 + e.button.button, 1);
break;
case SDL_MOUSEBUTTONUP:
inputButton(BTN_MOUSE0 + e.button.button, 0);
break;
case SDL_KEYDOWN:
inputButton(e.key.keysym.sym, 1);
break;
case SDL_KEYUP:
inputButton(e.key.keysym.sym, 0);
break;
}
}
if (grab) {
int mx=0, my=0;
SDL_GetMouseState(&mx, &my);
mx -= w/2;
my -= h/2;
if (mx*mx+my*my >= 8) {
SDL_WarpMouseInWindow(win, w/2, h/2);
inputMotion(mx, my);
}
}
render();
SDL_GL_SwapWindow(win);
SDL_Delay(1);
}
SDL_GL_DeleteContext(ctx);
SDL_DestroyWindow(win);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment