Create window
Program creates an OpenGL window that can be resized or go fullscreen. Nothing drawn inside the window yet.
This commit is contained in:
90
src/game.c
Normal file
90
src/game.c
Normal file
@@ -0,0 +1,90 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "GL/glew.h"
|
||||
#include "SDL2/SDL_opengl.h"
|
||||
#include "game.h"
|
||||
|
||||
GameState *Game_New()
|
||||
{
|
||||
GameState *gs = calloc(1, sizeof(GameState));
|
||||
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
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, 5);
|
||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||
|
||||
gs->window = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);;
|
||||
gs->glContext = SDL_GL_CreateContext(gs->window);
|
||||
printf("Created OpenGL window.\n");
|
||||
|
||||
glewInit();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
printf("Initialized OpenGL.\n");
|
||||
|
||||
gs->running = true;
|
||||
return gs;
|
||||
}
|
||||
|
||||
static void HandleKeyDown(GameState *gs, SDL_KeyCode sym)
|
||||
{
|
||||
switch (sym)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
gs->running = false;
|
||||
break;
|
||||
case SDLK_BACKQUOTE:
|
||||
SDL_bool currentMode = SDL_GetRelativeMouseMode();
|
||||
SDL_SetRelativeMouseMode(currentMode == SDL_FALSE ? SDL_TRUE : SDL_FALSE);
|
||||
break;
|
||||
case SDLK_F11:
|
||||
{
|
||||
Uint32 windowFlags = SDL_GetWindowFlags(gs->window);
|
||||
if ((windowFlags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0) windowFlags = 0;
|
||||
else windowFlags = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
||||
SDL_SetWindowFullscreen(gs->window, windowFlags);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void HandleKeyUp(GameState *gs, SDL_KeyCode sym)
|
||||
{
|
||||
}
|
||||
|
||||
void Game_Update(GameState *gs)
|
||||
{
|
||||
SDL_Event sdlEvent;
|
||||
|
||||
while (SDL_PollEvent(&sdlEvent))
|
||||
{
|
||||
switch (sdlEvent.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
printf("Event: SDL_QUIT\n");
|
||||
gs->running = false;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
if (sdlEvent.key.repeat == 0)
|
||||
{
|
||||
HandleKeyDown(gs, sdlEvent.key.keysym.sym);
|
||||
}
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
HandleKeyUp(gs, sdlEvent.key.keysym.sym);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Game_Destroy(GameState *gs)
|
||||
{
|
||||
SDL_GL_DeleteContext(gs->glContext);
|
||||
SDL_DestroyWindow(gs->window);
|
||||
free(gs);
|
||||
}
|
||||
12
src/game.h
Normal file
12
src/game.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bool running;
|
||||
SDL_Window *window;
|
||||
SDL_GLContext *glContext;
|
||||
} GameState;
|
||||
|
||||
GameState *Game_New();
|
||||
void Game_Update(GameState *gs);
|
||||
void Game_Destroy(GameState *gs);
|
||||
25
src/main.c
25
src/main.c
@@ -1,6 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include "SDL2/SDL.h"
|
||||
#include "SDL2/SDL_timer.h"
|
||||
#include "game.h"
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
printf("Hello World!\n");
|
||||
printf("Starting...\n");
|
||||
GameState *gs = Game_New();
|
||||
|
||||
if (gs == NULL)
|
||||
{
|
||||
printf("Failed to start.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (gs->running)
|
||||
{
|
||||
Game_Update(gs);
|
||||
SDL_Delay(100);
|
||||
}
|
||||
|
||||
printf("Shutting down...\n");
|
||||
Game_Destroy(gs);
|
||||
SDL_Quit();
|
||||
printf("Done.\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user