Replaced GLFW with SDL2 and GLEW with custom GL loader. Abstracted most platform code to single header and source files

dev
Shariq Shah 10 years ago
parent 80a1f36bbd
commit e2bf6e4816
  1. 2
      build/linux/makefile
  2. 3
      src/framebuffer.c
  3. 129
      src/game.c
  4. 13
      src/game.h
  5. 4
      src/geometry.c
  6. 42
      src/gl_load.c
  7. 60
      src/gl_load.h
  8. 243
      src/input.c
  9. 400
      src/input.h
  10. 40
      src/main.c
  11. 4
      src/model.c
  12. 277
      src/platform.c
  13. 43
      src/platform.h
  14. 19
      src/renderer.c
  15. 5
      src/renderer.h
  16. 4
      src/shader.c
  17. 3
      src/texture.c

@ -3,7 +3,7 @@ CC=gcc
SRC_DIR=../../src SRC_DIR=../../src
SRCS=$(patsubst $(SRC_DIR)/%.c, %.c, $(wildcard ../../src/*.c)) SRCS=$(patsubst $(SRC_DIR)/%.c, %.c, $(wildcard ../../src/*.c))
OBJS=$(patsubst %.c,%.o,$(SRCS)) OBJS=$(patsubst %.c,%.o,$(SRCS))
CFLAGS= -g -Wall CFLAGS= -o3 -Wall
LIBS=$(shell pkg-config --static --libs glfw3 glew) LIBS=$(shell pkg-config --static --libs glfw3 glew)
all: $(OBJS) all: $(OBJS)

@ -3,9 +3,8 @@
#include "renderer.h" #include "renderer.h"
#include "log.h" #include "log.h"
#include "texture.h" #include "texture.h"
#include "gl_load.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <assert.h> #include <assert.h>
struct FBO struct FBO

@ -2,10 +2,9 @@
#include <string.h> #include <string.h>
#include <stddef.h> #include <stddef.h>
#include <time.h> #include <time.h>
#include <GLFW/glfw3.h>
#include "game.h" #include "game.h"
#include "window_system.h" #include "platform.h"
#include "input.h" #include "input.h"
#include "renderer.h" #include "renderer.h"
#include "log.h" #include "log.h"
@ -23,28 +22,30 @@
#include "material.h" #include "material.h"
#include "framebuffer.h" #include "framebuffer.h"
#include "light.h" #include "light.h"
#include "gl_load.h"
void run(void); static void run(void);
void update(float dt); static void update(float dt);
void render(void); static void render(void);
void debug(float dt); static void debug(float dt);
void scene_setup(void); static void scene_setup(void);
int player_node = -1; static struct Game_State* game_state = NULL;
int player_pitch_node = -1;
void game_init(void) void game_init(struct Window* window)
{ {
/* TODO: Implement dealing with init failures */ /* TODO: Implement dealing with init failures */
GLFWwindow* window = window_get_active(); game_state = malloc(sizeof(*game_state));
game_state->window = window;
/* Init systems */ /* Init systems */
input_init(window); input_init();
io_file_init("/mnt/Dev/Projects/symmetry/assets/");/* TODO: Implement proper way of getting binary directory */ io_file_init("/mnt/Dev/Projects/symmetry/assets/");/* TODO: Implement proper way of getting binary directory */
shader_init(); shader_init();
texture_init(); texture_init();
framebuffer_init(); framebuffer_init();
geom_init(); geom_init();
renderer_init(window); renderer_init();
transform_init(); transform_init();
light_init(); light_init();
camera_init(); camera_init();
@ -61,17 +62,17 @@ void game_init(void)
void scene_setup(void) void scene_setup(void)
{ {
int forward_keys[2] = {'W', GLFW_KEY_UP}; int forward_keys[2] = {KEY_W, KEY_UP};
int backward_keys[2] = {'S', GLFW_KEY_DOWN}; int backward_keys[2] = {KEY_S, KEY_DOWN};
int up_keys[2] = {'Q'}; int up_keys[2] = {KEY_Q};
int down_keys[2] = {'E'}; int down_keys[2] = {KEY_E};
int left_keys[2] = {'A', GLFW_KEY_LEFT}; int left_keys[2] = {KEY_A, KEY_LEFT};
int right_keys[2] = {'D', GLFW_KEY_RIGHT}; int right_keys[2] = {KEY_D, KEY_RIGHT};
int turn_right_keys[1] = {'L'}; int turn_right_keys[1] = {KEY_L};
int turn_left_keys[1] = {'J'}; int turn_left_keys[1] = {KEY_J};
int turn_up_keys[1] = {'I'}; int turn_up_keys[1] = {KEY_I};
int turn_down_keys[1] = {'K'}; int turn_down_keys[1] = {KEY_K};
int sprint_keys[2] = {GLFW_KEY_LEFT_SHIFT, GLFW_KEY_RIGHT_SHIFT}; int sprint_keys[2] = {KEY_LSHIFT, KEY_RSHIFT};
input_map_create("Move_Forward", forward_keys, 2); input_map_create("Move_Forward", forward_keys, 2);
input_map_create("Move_Backward", backward_keys, 2); input_map_create("Move_Backward", backward_keys, 2);
input_map_create("Move_Up", up_keys, 1); input_map_create("Move_Up", up_keys, 1);
@ -85,8 +86,8 @@ void scene_setup(void)
input_map_create("Sprint", sprint_keys, 2); input_map_create("Sprint", sprint_keys, 2);
struct Entity* player = scene_add_new("player", "None"); struct Entity* player = scene_add_new("player", "None");
player_node = player->node; game_state->player_node = player->node;
vec3 viewer_pos = {5, 4, 20}; vec3 viewer_pos = {10, 4, 100};
struct Transform* viewer_tran = entity_component_get(player, C_TRANSFORM); struct Transform* viewer_tran = entity_component_get(player, C_TRANSFORM);
struct Model* player_model = entity_component_add(player, C_MODEL, "sphere.pamesh", NULL); struct Model* player_model = entity_component_add(player, C_MODEL, "sphere.pamesh", NULL);
vec4 color = {0.f, 1.f, 1.f, 1.f }; vec4 color = {0.f, 1.f, 1.f, 1.f };
@ -180,7 +181,7 @@ void scene_setup(void)
void debug(float dt) void debug(float dt)
{ {
//struct Entity* entity = entity_get(player_node); //struct Entity* entity = entity_get(player_node);
struct Entity* entity = !input_key_state_get('C', GLFW_PRESS) ? entity_get(player_node) : scene_find("Screen_Camera"); struct Entity* entity = !input_key_state_get('C', KS_PRESSED) ? entity_get(game_state->player_node) : scene_find("Screen_Camera");
struct Camera* cam = entity_component_get(entity, C_CAMERA); struct Camera* cam = entity_component_get(entity, C_CAMERA);
camera_set_primary_viewer(cam); camera_set_primary_viewer(cam);
struct Transform* transform = entity_component_get(entity, C_TRANSFORM); struct Transform* transform = entity_component_get(entity, C_TRANSFORM);
@ -194,24 +195,25 @@ void debug(float dt)
vec3 rot_axis_left_right = {0, 1, 0}; vec3 rot_axis_left_right = {0, 1, 0};
/* Look around */ /* Look around */
if(input_map_state_get("Turn_Up", GLFW_PRESS)) turn_up_down += turn_speed; if(input_map_state_get("Turn_Up", KS_PRESSED)) turn_up_down += turn_speed;
if(input_map_state_get("Turn_Down", GLFW_PRESS)) turn_up_down -= turn_speed; if(input_map_state_get("Turn_Down", KS_PRESSED)) turn_up_down -= turn_speed;
if(input_map_state_get("Turn_Right", GLFW_PRESS)) turn_left_right += turn_speed; if(input_map_state_get("Turn_Right", KS_PRESSED)) turn_left_right += turn_speed;
if(input_map_state_get("Turn_Left", GLFW_PRESS)) turn_left_right -= turn_speed; if(input_map_state_get("Turn_Left", KS_PRESSED)) turn_left_right -= turn_speed;
if(input_mousebutton_state_get(GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS)) if(input_mousebutton_state_get(MB_RIGHT, KS_PRESSED))
{ {
input_cursor_mode_set(CM_LOCKED); if(input_mouse_mode_get() != MM_RELATIVE) input_mouse_mode_set(MM_RELATIVE);
const double scale = 0.25; const double scale = 0.25;
double cursor_lr, cursor_ud; int cursor_lr, cursor_ud;
input_cursor_pos_get(&cursor_lr, &cursor_ud); input_mouse_pos_get(&cursor_lr, &cursor_ud);
log_message("Mouse position : %d, %d", cursor_lr, cursor_ud);
turn_up_down = -cursor_ud * turn_speed * dt * scale; turn_up_down = -cursor_ud * turn_speed * dt * scale;
turn_left_right = cursor_lr * turn_speed * dt * scale; turn_left_right = cursor_lr * turn_speed * dt * scale;
input_cursor_pos_set(0.0, 0.0); input_mouse_pos_set(0.0, 0.0);
} }
else else
{ {
input_cursor_mode_set(CM_NORMAL); input_mouse_mode_set(MM_NORMAL);
turn_up_down *= dt; turn_up_down *= dt;
turn_left_right *= dt; turn_left_right *= dt;
} }
@ -254,13 +256,13 @@ void debug(float dt)
} }
/* Movement */ /* Movement */
if(input_map_state_get("Sprint", GLFW_PRESS)) move_speed *= move_scale; if(input_map_state_get("Sprint", KS_PRESSED)) move_speed *= move_scale;
if(input_map_state_get("Move_Forward", GLFW_PRESS)) offset.z -= move_speed; if(input_map_state_get("Move_Forward", KS_PRESSED)) offset.z -= move_speed;
if(input_map_state_get("Move_Backward", GLFW_PRESS)) offset.z += move_speed; if(input_map_state_get("Move_Backward", KS_PRESSED)) offset.z += move_speed;
if(input_map_state_get("Move_Left", GLFW_PRESS)) offset.x -= move_speed; if(input_map_state_get("Move_Left", KS_PRESSED)) offset.x -= move_speed;
if(input_map_state_get("Move_Right", GLFW_PRESS)) offset.x += move_speed; if(input_map_state_get("Move_Right", KS_PRESSED)) offset.x += move_speed;
if(input_map_state_get("Move_Up", GLFW_PRESS)) offset.y += move_speed; if(input_map_state_get("Move_Up", KS_PRESSED)) offset.y += move_speed;
if(input_map_state_get("Move_Down", GLFW_PRESS)) offset.y -= move_speed; if(input_map_state_get("Move_Down", KS_PRESSED)) offset.y -= move_speed;
vec3_scale(&offset, &offset, dt); vec3_scale(&offset, &offset, dt);
if(offset.x != 0 || offset.y != 0 || offset.z != 0) if(offset.x != 0 || offset.y != 0 || offset.z != 0)
@ -269,7 +271,7 @@ void debug(float dt)
log_message("Position : %s", tostr_vec3(&transform->position)); log_message("Position : %s", tostr_vec3(&transform->position));
} }
if(input_key_state_get(GLFW_KEY_SPACE, GLFW_PRESS)) if(input_key_state_get(KEY_SPACE, KS_PRESSED))
{ {
struct Entity* model = scene_find("Model_Entity"); struct Entity* model = scene_find("Model_Entity");
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
@ -277,7 +279,7 @@ void debug(float dt)
transform_rotate(mod_tran, &x_axis, 25.f * dt, TS_WORLD); transform_rotate(mod_tran, &x_axis, 25.f * dt, TS_WORLD);
} }
if(input_key_state_get(GLFW_KEY_M, GLFW_PRESS)) if(input_key_state_get(KEY_M, KS_PRESSED))
{ {
struct Entity* model = scene_find("Model_Entity"); struct Entity* model = scene_find("Model_Entity");
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
@ -287,7 +289,7 @@ void debug(float dt)
transform_translate(mod_tran, &amount, TS_LOCAL); transform_translate(mod_tran, &amount, TS_LOCAL);
} }
if(input_key_state_get(GLFW_KEY_N, GLFW_PRESS)) if(input_key_state_get(KEY_N, KS_PRESSED))
{ {
struct Entity* model = scene_find("Model_Entity"); struct Entity* model = scene_find("Model_Entity");
struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM); struct Transform* mod_tran = entity_component_get(model, C_TRANSFORM);
@ -297,41 +299,42 @@ void debug(float dt)
transform_translate(mod_tran, &amount, TS_LOCAL); transform_translate(mod_tran, &amount, TS_LOCAL);
} }
/* if(input_key_state_get(GLFW_KEY_C, GLFW_PRESS)) */ /* if(input_key_state_get(GLFW_KEY_C, KS_PRESSED)) */
/* { */ /* { */
/* struct Entity* cam_ent = scene_find("Screen_Camera"); */ /* struct Entity* cam_ent = scene_find("Screen_Camera"); */
/* struct Camera* cam = entity_component_get(cam_ent, C_CAMERA); */ /* struct Camera* cam = entity_component_get(cam_ent, C_CAMERA); */
/* camera_set_primary_viewer(cam); */ /* camera_set_primary_viewer(cam); */
/* } */ /* } */
/* if(input_key_state_get(GLFW_KEY_V, GLFW_PRESS)) */ /* if(input_key_state_get(GLFW_KEY_V, KS_PRESSED)) */
/* { */ /* { */
/* struct Camera* cam = entity_component_get(entity, C_CAMERA); */ /* struct Camera* cam = entity_component_get(entity, C_CAMERA); */
/* camera_set_primary_viewer(cam); */ /* camera_set_priimary_viewer(cam); */
/* } */ /* } */
} }
void run(void) void run(void)
{ {
double last_time = glfwGetTime(); uint32 last_time = platform_get_ticks();
while(!window_should_close()) int should_window_close = 0;
while(!should_window_close)
{ {
double curr_time = glfwGetTime(); uint32 curr_time = platform_get_ticks();
float delta_time = (float)(curr_time - last_time); float delta_time = (float)(curr_time - last_time) / 1000.f;
last_time = curr_time; last_time = curr_time;
update(delta_time); update(delta_time);
render(); render();
window_swap_buffers(); window_swap_buffers(game_state->window);
window_poll_events(); platform_poll_events(&should_window_close);
} }
} }
void update(float dt) void update(float dt)
{ {
input_update(); input_update();
if(input_key_state_get(GLFW_KEY_ESCAPE, GLFW_PRESS)) //if(input_key_state_get(KEY_ESCAPE, KS_PRESSED))
window_set_should_close(1); //window_set_should_close(1);
debug(dt); debug(dt);
} }
@ -357,4 +360,14 @@ void game_cleanup(void)
framebuffer_cleanup(); framebuffer_cleanup();
texture_cleanup(); texture_cleanup();
shader_cleanup(); shader_cleanup();
window_destroy(game_state->window);
gl_cleanup();
window_cleanup();
platform_cleanup();
free(game_state);
}
struct Game_State* game_state_get(void)
{
return game_state;
} }

@ -1,7 +1,18 @@
#ifndef game_H #ifndef game_H
#define game_H #define game_H
void game_init(void); #include "platform.h"
struct Game_State
{
struct Window* window;
int player_node;
int player_pitch_node;
};
struct Game_State* game_state_get(void);
void game_init(struct Window* window);
void game_cleanup(void); void game_cleanup(void);
#endif #endif

@ -6,9 +6,7 @@
#include "renderer.h" #include "renderer.h"
#include "bounding_volumes.h" #include "bounding_volumes.h"
#include "transform.h" #include "transform.h"
#include "gl_load.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>

@ -0,0 +1,42 @@
#include "gl_load.h"
#include "log.h"
#include <SDL.h>
#define GLE(ret, name, ...) name##proc * gl##name;
SYMMETRY_GL_LIST
#undef GLE
int gl_load_library(void)
{
int success = 1;
if(SDL_GL_LoadLibrary(NULL) < 0)
{
success = 0;
log_error("gl_load_library", "Failed to load GL library %s", SDL_GetError());
}
else
{
log_message("Loaded GL library");
}
return success;
}
int gl_load_extentions(void)
{
int success = 1;
#define GLE(ret, name, ...) \
gl##name = (name##proc *) SDL_GL_GetProcAddress("gl" #name); \
if (!gl##name) { \
log_error("gl_load_extentions:SDL_GL_GetProcAddress", "Function gl" #name " couldn't be loaded from libGL.so"); \
success = 0; \
return success; \
}
SYMMETRY_GL_LIST
#undef GLE
return success;
}
void gl_cleanup(void)
{
SDL_GL_UnloadLibrary();
}

@ -0,0 +1,60 @@
#ifndef GL_LOAD_H
#define GL_LOAD_H
#include <SDL2/SDL_opengl.h>
#define SYMMETRY_GL_LIST \
/* ret, name, params */ \
GLE(void, LinkProgram, GLuint program) \
GLE(void, GetProgramiv, GLuint program, GLenum pname, GLint *params) \
GLE(GLuint, CreateShader, GLenum type) \
GLE(void, ShaderSource, GLuint shader, GLsizei count, const GLchar* const *string, const GLint *length) \
GLE(void, CompileShader, GLuint shader) \
GLE(void, GetShaderiv, GLuint shader, GLenum pname, GLint *params) \
GLE(void, GetShaderInfoLog, GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog) \
GLE(void, DeleteShader, GLuint shader) \
GLE(GLuint, CreateProgram, void) \
GLE(void, AttachShader, GLuint program, GLuint shader) \
GLE(void, DetachShader, GLuint program, GLuint shader) \
GLE(void, UseProgram, GLuint program) \
GLE(void, DeleteProgram, GLuint program) \
GLE(void, GenVertexArrays, GLsizei n, GLuint *arrays) \
GLE(void, BindVertexArray, GLuint array) \
GLE(void, BufferData, GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) \
GLE(void, GenBuffers, GLsizei n, GLuint *buffers) \
GLE(void, BindBuffer, GLenum target, GLuint buffer) \
GLE(void, DeleteBuffers, GLsizei n, const GLuint *buffers) \
GLE(void, BindAttribLocation, GLuint program, GLuint index, const GLchar *name) \
GLE(GLint, GetUniformLocation, GLuint program, const GLchar *name) \
GLE(void, Uniform1i, GLint location, GLint v0) \
GLE(void, Uniform1f, GLint location, GLfloat v0) \
GLE(void, Uniform2fv, GLint location, GLsizei count, const GLfloat* value) \
GLE(void, Uniform3fv, GLint location, GLsizei count, const GLfloat* value) \
GLE(void, Uniform4f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) \
GLE(void, Uniform4fv, GLint location, GLsizei count, const GLfloat *value) \
GLE(void, UniformMatrix4fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) \
GLE(void, DeleteVertexArrays, GLsizei n, const GLuint *arrays) \
GLE(void, EnableVertexAttribArray, GLuint index) \
GLE(void, VertexAttribPointer, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) \
GLE(void, GenFramebuffers, GLsizei n, GLuint* framebuffers) \
GLE(void, GenRenderbuffers, GLsizei n, GLuint* renderbuffers) \
GLE(void, BindFramebuffer, GLenum target, GLuint framebuffer) \
GLE(void, BindRenderbuffer, GLenum target, GLuint renderbuffer) \
GLE(void, RenderbufferStorage, GLenum target, GLenum internalformat, GLsizei width, GLsizei height) \
GLE(void, FramebufferRenderbuffer, GLuint framebuffer, GLenum attachment, GLenum renderbuffertarget, GLuint renderbuffer) \
GLE(GLenum, CheckFramebufferStatus, GLenum target) \
GLE(void, DeleteFramebuffers, GLsizei n, const GLuint* framebuffers) \
GLE(void, DeleteRenderbuffers, GLsizei n, const GLuint* renderbuffers) \
GLE(void, FramebufferTexture2D, GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level) \
GLE(void, GetProgramInfoLog, GLuint program, GLsizei bufSize, GLsizei* length, GLchar* infoLog) \
/* end */
#define GLE(ret, name, ...) typedef ret APIENTRY name##proc(__VA_ARGS__); extern name##proc * gl##name;
SYMMETRY_GL_LIST
#undef GLE
int gl_load_library(void);
int gl_load_extentions(void);
void gl_cleanup(void);
#endif

@ -3,13 +3,11 @@
#include <string.h> #include <string.h>
#include "input.h" #include "input.h"
#include "array.h" #include "array.h"
#include "GLFW/glfw3.h" #include "platform.h"
#include "window_system.h"
#include "log.h" #include "log.h"
#define KS_INACTIVE -1; /* state for input map is set to KS_INACTIVE(KeyState_Inactive) when /* #define KS_INACTIVE -1; /\* state for input map is set to KS_INACTIVE(KeyState_Inactive) when */
the key is neither pressed nor released */ /* the key is neither pressed nor released *\/ */
struct Input_Map struct Input_Map
{ {
@ -18,20 +16,18 @@ struct Input_Map
int state; int state;
}; };
static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods); static void input_on_key(int key, int scancode, int state, int mod_ctrl, int mod_shift);
static void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods); static void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks);
static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos); static void input_on_mouse_motion(int x, int y, int xrel, int yrel);
static int map_find(const char* name); static int map_find(const char* name);
static const char* get_key_name(int key);
static struct Input_Map* input_map_list; static struct Input_Map* input_map_list;
void input_init(GLFWwindow* window) void input_init(void)
{ {
glfwSetMouseButtonCallback(window, input_on_mousebutton); platform_keyboard_callback_set(&input_on_key);
glfwSetKeyCallback(window, input_on_key); platform_mousebutton_callback_set(&input_on_mousebutton);
glfwSetCursorPosCallback(window, input_on_cursor_move); platform_mousemotion_callback_set(&input_on_mouse_motion);
//glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
input_map_list = array_new(struct Input_Map); input_map_list = array_new(struct Input_Map);
} }
@ -46,25 +42,23 @@ void input_cleanup(void)
array_free(input_map_list); array_free(input_map_list);
} }
static void input_on_cursor_move(GLFWwindow* window, double xpos, double ypos) void input_on_mouse_motion(int x, int y, int xrel, int yrel)
{ {
} }
void input_cursor_pos_get(double* xpos, double* ypos) void input_mouse_pos_get(int* xpos, int* ypos)
{ {
assert(xpos && ypos); assert(xpos && ypos);
GLFWwindow* window = window_get_active(); platform_mouse_position_get(xpos, ypos);
glfwGetCursorPos(window, xpos, ypos);
} }
void input_cursor_pos_set(double xpos, double ypos) void input_mouse_pos_set(int xpos, int ypos)
{ {
GLFWwindow* window = window_get_active(); platform_mouse_global_position_set(xpos, ypos);
glfwSetCursorPos(window, xpos, ypos);
} }
static void input_on_key(GLFWwindow* window, int key, int scancode, int action, int mods) void input_on_key(int key, int scancode, int state, int mod_ctrl, int mod_shift)
{ {
for(int i = 0; i < array_len(input_map_list); i++) for(int i = 0; i < array_len(input_map_list); i++)
{ {
@ -73,30 +67,23 @@ static void input_on_key(GLFWwindow* window, int key, int scancode, int action,
{ {
if(map->keys[j] == key) if(map->keys[j] == key)
{ {
map->state = action; map->state = state;
break; break;
} }
} }
} }
} }
static void input_on_mousebutton(GLFWwindow* window, int button, int action, int mods) void input_on_mousebutton(int button, int state, int x, int y, int8 num_clicks)
{ {
/* Probably add 'mouse maps', same as input maps for keyvboard but with buttons /* Probably add 'mouse maps', same as input maps for keyvboard but with buttons
Do we even need that? Do we even need that?
*/ */
} }
void input_cursor_mode_set(enum Cursor_Mode mode) void input_mouse_mode_set(enum Mouse_Mode mode)
{ {
GLFWwindow* window = window_get_active(); platform_mouse_relative_mode_set(mode == MM_NORMAL ? 0 : 1);
int cursor_mode = GLFW_CURSOR_NORMAL;
if(mode == CM_HIDDEN)
cursor_mode = GLFW_CURSOR_HIDDEN;
else if(mode == CM_LOCKED)
cursor_mode = GLFW_CURSOR_DISABLED;
glfwSetInputMode(window, GLFW_CURSOR, cursor_mode);
} }
int input_map_state_get(const char* map_name, int state) int input_map_state_get(const char* map_name, int state)
@ -112,51 +99,37 @@ int input_map_state_get(const char* map_name, int state)
} }
} }
if(state == GLFW_PRESS) int result = 0;
if(current_state == state)
{ {
if(current_state == GLFW_PRESS || current_state == GLFW_REPEAT) result = 1;
return 1;
else
return 0;
}
else
{
return state == current_state ? 1 : 0;
} }
return result;
//return state == current_state ? 1 : 0;
/* if(state == KS_PRESSED) */
/* { */
/* if(current_state == KS_PRESSED)// || current_state == GLFW_REPEAT) */
/* return 1; */
/* else */
/* return 0; */
/* } */
/* else */
/* { */
/* return state == current_state ? 1 : 0; */
/* } */
} }
int input_key_state_get(int key, int state_type) int input_key_state_get(int key, int state_type)
{ {
GLFWwindow* window = window_get_active(); int current_state = platform_key_state_get(key);
int current_state = glfwGetKey(window, key);
if(state_type == GLFW_PRESS)
{
if(current_state == GLFW_PRESS || current_state == GLFW_REPEAT)
return 1;
else
return 0;
}
else
{
return state_type == current_state ? 1 : 0; return state_type == current_state ? 1 : 0;
}
} }
int input_mousebutton_state_get(int button, int state_type) int input_mousebutton_state_get(uint button, int state_type)
{ {
GLFWwindow* window = window_get_active(); int current_state = platform_mousebutton_state_get(button);
int current_state = glfwGetMouseButton(window, button);
if(state_type == GLFW_PRESS)
{
if(current_state == GLFW_PRESS || current_state == GLFW_REPEAT)
return 1;
else
return 0;
}
else
{
return state_type == current_state ? 1 : 0; return state_type == current_state ? 1 : 0;
}
} }
void input_map_create(const char* name, int* keys, size_t num_keys) void input_map_create(const char* name, int* keys, size_t num_keys)
@ -178,7 +151,7 @@ void input_update(void)
for(int i = 0; i < array_len(input_map_list); i++) for(int i = 0; i < array_len(input_map_list); i++)
{ {
struct Input_Map* map = &input_map_list[i]; struct Input_Map* map = &input_map_list[i];
if(map->state == GLFW_RELEASE) if(map->state == KS_RELEASED)
map->state = KS_INACTIVE; map->state = KS_INACTIVE;
} }
} }
@ -246,133 +219,11 @@ static int map_find(const char* name)
return index; return index;
} }
static const char* get_key_name(int key) int input_mouse_mode_get(void)
{ {
switch (key) int mouse_mode = MM_NORMAL;
{ if(platform_mouse_relative_mode_get()) mouse_mode = MM_RELATIVE;
// Printable keys return mouse_mode;
case GLFW_KEY_A: return "A";
case GLFW_KEY_B: return "B";
case GLFW_KEY_C: return "C";
case GLFW_KEY_D: return "D";
case GLFW_KEY_E: return "E";
case GLFW_KEY_F: return "F";
case GLFW_KEY_G: return "G";
case GLFW_KEY_H: return "H";
case GLFW_KEY_I: return "I";
case GLFW_KEY_J: return "J";
case GLFW_KEY_K: return "K";
case GLFW_KEY_L: return "L";
case GLFW_KEY_M: return "M";
case GLFW_KEY_N: return "N";
case GLFW_KEY_O: return "O";
case GLFW_KEY_P: return "P";
case GLFW_KEY_Q: return "Q";
case GLFW_KEY_R: return "R";
case GLFW_KEY_S: return "S";
case GLFW_KEY_T: return "T";
case GLFW_KEY_U: return "U";
case GLFW_KEY_V: return "V";
case GLFW_KEY_W: return "W";
case GLFW_KEY_X: return "X";
case GLFW_KEY_Y: return "Y";
case GLFW_KEY_Z: return "Z";
case GLFW_KEY_1: return "1";
case GLFW_KEY_2: return "2";
case GLFW_KEY_3: return "3";
case GLFW_KEY_4: return "4";
case GLFW_KEY_5: return "5";
case GLFW_KEY_6: return "6";
case GLFW_KEY_7: return "7";
case GLFW_KEY_8: return "8";
case GLFW_KEY_9: return "9";
case GLFW_KEY_0: return "0";
case GLFW_KEY_SPACE: return "SPACE";
case GLFW_KEY_MINUS: return "MINUS";
case GLFW_KEY_EQUAL: return "EQUAL";
case GLFW_KEY_LEFT_BRACKET: return "LEFT BRACKET";
case GLFW_KEY_RIGHT_BRACKET: return "RIGHT BRACKET";
case GLFW_KEY_BACKSLASH: return "BACKSLASH";
case GLFW_KEY_SEMICOLON: return "SEMICOLON";
case GLFW_KEY_APOSTROPHE: return "APOSTROPHE";
case GLFW_KEY_GRAVE_ACCENT: return "GRAVE ACCENT";
case GLFW_KEY_COMMA: return "COMMA";
case GLFW_KEY_PERIOD: return "PERIOD";
case GLFW_KEY_SLASH: return "SLASH";
case GLFW_KEY_WORLD_1: return "WORLD 1";
case GLFW_KEY_WORLD_2: return "WORLD 2";
// Function keys
case GLFW_KEY_ESCAPE: return "ESCAPE";
case GLFW_KEY_F1: return "F1";
case GLFW_KEY_F2: return "F2";
case GLFW_KEY_F3: return "F3";
case GLFW_KEY_F4: return "F4";
case GLFW_KEY_F5: return "F5";
case GLFW_KEY_F6: return "F6";
case GLFW_KEY_F7: return "F7";
case GLFW_KEY_F8: return "F8";
case GLFW_KEY_F9: return "F9";
case GLFW_KEY_F10: return "F10";
case GLFW_KEY_F11: return "F11";
case GLFW_KEY_F12: return "F12";
case GLFW_KEY_F13: return "F13";
case GLFW_KEY_F14: return "F14";
case GLFW_KEY_F15: return "F15";
case GLFW_KEY_F16: return "F16";
case GLFW_KEY_F17: return "F17";
case GLFW_KEY_F18: return "F18";
case GLFW_KEY_F19: return "F19";
case GLFW_KEY_F20: return "F20";
case GLFW_KEY_F21: return "F21";
case GLFW_KEY_F22: return "F22";
case GLFW_KEY_F23: return "F23";
case GLFW_KEY_F24: return "F24";
case GLFW_KEY_F25: return "F25";
case GLFW_KEY_UP: return "UP";
case GLFW_KEY_DOWN: return "DOWN";
case GLFW_KEY_LEFT: return "LEFT";
case GLFW_KEY_RIGHT: return "RIGHT";
case GLFW_KEY_LEFT_SHIFT: return "LEFT SHIFT";
case GLFW_KEY_RIGHT_SHIFT: return "RIGHT SHIFT";
case GLFW_KEY_LEFT_CONTROL: return "LEFT CONTROL";
case GLFW_KEY_RIGHT_CONTROL: return "RIGHT CONTROL";
case GLFW_KEY_LEFT_ALT: return "LEFT ALT";
case GLFW_KEY_RIGHT_ALT: return "RIGHT ALT";
case GLFW_KEY_TAB: return "TAB";
case GLFW_KEY_ENTER: return "ENTER";
case GLFW_KEY_BACKSPACE: return "BACKSPACE";
case GLFW_KEY_INSERT: return "INSERT";
case GLFW_KEY_DELETE: return "DELETE";
case GLFW_KEY_PAGE_UP: return "PAGE UP";
case GLFW_KEY_PAGE_DOWN: return "PAGE DOWN";
case GLFW_KEY_HOME: return "HOME";
case GLFW_KEY_END: return "END";
case GLFW_KEY_KP_0: return "KEYPAD 0";
case GLFW_KEY_KP_1: return "KEYPAD 1";
case GLFW_KEY_KP_2: return "KEYPAD 2";
case GLFW_KEY_KP_3: return "KEYPAD 3";
case GLFW_KEY_KP_4: return "KEYPAD 4";
case GLFW_KEY_KP_5: return "KEYPAD 5";
case GLFW_KEY_KP_6: return "KEYPAD 6";
case GLFW_KEY_KP_7: return "KEYPAD 7";
case GLFW_KEY_KP_8: return "KEYPAD 8";
case GLFW_KEY_KP_9: return "KEYPAD 9";
case GLFW_KEY_KP_DIVIDE: return "KEYPAD DIVIDE";
case GLFW_KEY_KP_MULTIPLY: return "KEYPAD MULTPLY";
case GLFW_KEY_KP_SUBTRACT: return "KEYPAD SUBTRACT";
case GLFW_KEY_KP_ADD: return "KEYPAD ADD";
case GLFW_KEY_KP_DECIMAL: return "KEYPAD DECIMAL";
case GLFW_KEY_KP_EQUAL: return "KEYPAD EQUAL";
case GLFW_KEY_KP_ENTER: return "KEYPAD ENTER";
case GLFW_KEY_PRINT_SCREEN: return "PRINT SCREEN";
case GLFW_KEY_NUM_LOCK: return "NUM LOCK";
case GLFW_KEY_CAPS_LOCK: return "CAPS LOCK";
case GLFW_KEY_SCROLL_LOCK: return "SCROLL LOCK";
case GLFW_KEY_PAUSE: return "PAUSE";
case GLFW_KEY_LEFT_SUPER: return "LEFT SUPER";
case GLFW_KEY_RIGHT_SUPER: return "RIGHT SUPER";
case GLFW_KEY_MENU: return "MENU";
default: return "UNKNOWN";
}
} }

@ -2,24 +2,404 @@
#define input_H #define input_H
#include <stdlib.h> #include <stdlib.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_events.h>
typedef struct GLFWwindow GLFWwindow; enum Key_State
{
KS_INACTIVE = -1,
KS_PRESSED = SDL_PRESSED,
KS_RELEASED = SDL_RELEASED
};
enum Mouse_Mode
{
MM_NORMAL = 0,
MM_RELATIVE
};
enum Cursor_Mode enum Mouse_Button
{ {
CM_NORMAL = 0, MB_LEFT = SDL_BUTTON_LEFT,
CM_LOCKED, MB_MIDDLE = SDL_BUTTON_MIDDLE,
CM_HIDDEN MB_RIGHT = SDL_BUTTON_RIGHT,
MB_X1 = SDL_BUTTON_X1,
MB_X2 = SDL_BUTTON_X2
};
enum Keyboard_Key
{
KEY_A = SDLK_a,
KEY_B = SDLK_b,
KEY_C = SDLK_c,
KEY_D = SDLK_d,
KEY_E = SDLK_e,
KEY_F = SDLK_f,
KEY_G = SDLK_g,
KEY_H = SDLK_h,
KEY_I = SDLK_i,
KEY_J = SDLK_j,
KEY_K = SDLK_k,
KEY_L = SDLK_l,
KEY_M = SDLK_m,
KEY_N = SDLK_n,
KEY_O = SDLK_o,
KEY_P = SDLK_p,
KEY_Q = SDLK_q,
KEY_R = SDLK_r,
KEY_S = SDLK_s,
KEY_T = SDLK_t,
KEY_U = SDLK_u,
KEY_V = SDLK_v,
KEY_W = SDLK_w,
KEY_X = SDLK_x,
KEY_Y = SDLK_y,
KEY_Z = SDLK_z,
KEY_0 = SDLK_0,
KEY_1 = SDLK_1,
KEY_2 = SDLK_2,
KEY_3 = SDLK_3,
KEY_4 = SDLK_4,
KEY_5 = SDLK_5,
KEY_6 = SDLK_6,
KEY_7 = SDLK_7,
KEY_8 = SDLK_8,
KEY_9 = SDLK_9,
KEY_BACKSPACE = SDLK_BACKSPACE,
KEY_TAB = SDLK_TAB,
KEY_RETURN = SDLK_RETURN,
KEY_RETURN2 = SDLK_RETURN2,
KEY_KP_ENTER = SDLK_KP_ENTER,
KEY_SHIFT = SDLK_LSHIFT,
KEY_CTRL = SDLK_LCTRL,
KEY_ALT = SDLK_LALT,
KEY_GUI = SDLK_LGUI,
KEY_PAUSE = SDLK_PAUSE,
KEY_CAPSLOCK = SDLK_CAPSLOCK,
KEY_ESCAPE = SDLK_ESCAPE,
KEY_SPACE = SDLK_SPACE,
KEY_PAGEUP = SDLK_PAGEUP,
KEY_PAGEDOWN = SDLK_PAGEDOWN,
KEY_END = SDLK_END,
KEY_HOME = SDLK_HOME,
KEY_LEFT = SDLK_LEFT,
KEY_UP = SDLK_UP,
KEY_RIGHT = SDLK_RIGHT,
KEY_DOWN = SDLK_DOWN,
KEY_SELECT = SDLK_SELECT,
KEY_PRINTSCREEN = SDLK_PRINTSCREEN,
KEY_INSERT = SDLK_INSERT,
KEY_DELETE = SDLK_DELETE,
KEY_LGUI = SDLK_LGUI,
KEY_RGUI = SDLK_RGUI,
KEY_APPLICATION = SDLK_APPLICATION,
KEY_KP_0 = SDLK_KP_0,
KEY_KP_1 = SDLK_KP_1,
KEY_KP_2 = SDLK_KP_2,
KEY_KP_3 = SDLK_KP_3,
KEY_KP_4 = SDLK_KP_4,
KEY_KP_5 = SDLK_KP_5,
KEY_KP_6 = SDLK_KP_6,
KEY_KP_7 = SDLK_KP_7,
KEY_KP_8 = SDLK_KP_8,
KEY_KP_9 = SDLK_KP_9,
KEY_KP_MULTIPLY = SDLK_KP_MULTIPLY,
KEY_KP_PLUS = SDLK_KP_PLUS,
KEY_KP_MINUS = SDLK_KP_MINUS,
KEY_KP_PERIOD = SDLK_KP_PERIOD,
KEY_KP_DIVIDE = SDLK_KP_DIVIDE,
KEY_F1 = SDLK_F1,
KEY_F2 = SDLK_F2,
KEY_F3 = SDLK_F3,
KEY_F4 = SDLK_F4,
KEY_F5 = SDLK_F5,
KEY_F6 = SDLK_F6,
KEY_F7 = SDLK_F7,
KEY_F8 = SDLK_F8,
KEY_F9 = SDLK_F9,
KEY_F10 = SDLK_F10,
KEY_F11 = SDLK_F11,
KEY_F12 = SDLK_F12,
KEY_F13 = SDLK_F13,
KEY_F14 = SDLK_F14,
KEY_F15 = SDLK_F15,
KEY_F16 = SDLK_F16,
KEY_F17 = SDLK_F17,
KEY_F18 = SDLK_F18,
KEY_F19 = SDLK_F19,
KEY_F20 = SDLK_F20,
KEY_F21 = SDLK_F21,
KEY_F22 = SDLK_F22,
KEY_F23 = SDLK_F23,
KEY_F24 = SDLK_F24,
KEY_NUMLOCKCLEAR = SDLK_NUMLOCKCLEAR,
KEY_SCROLLLOCK = SDLK_SCROLLLOCK,
KEY_LSHIFT = SDLK_LSHIFT,
KEY_RSHIFT = SDLK_RSHIFT,
KEY_LCTRL = SDLK_LCTRL,
KEY_RCTRL = SDLK_RCTRL,
KEY_LALT = SDLK_LALT,
KEY_RALT = SDLK_RALT
};
enum Keyboard_Scancode
{
SCANCODE_UNKNOWN = SDL_SCANCODE_UNKNOWN,
SCANCODE_CTRL = SDL_SCANCODE_LCTRL,
SCANCODE_SHIFT = SDL_SCANCODE_LSHIFT,
SCANCODE_ALT = SDL_SCANCODE_LALT,
SCANCODE_GUI = SDL_SCANCODE_LGUI,
SCANCODE_A = SDL_SCANCODE_A,
SCANCODE_B = SDL_SCANCODE_B,
SCANCODE_C = SDL_SCANCODE_C,
SCANCODE_D = SDL_SCANCODE_D,
SCANCODE_E = SDL_SCANCODE_E,
SCANCODE_F = SDL_SCANCODE_F,
SCANCODE_G = SDL_SCANCODE_G,
SCANCODE_H = SDL_SCANCODE_H,
SCANCODE_I = SDL_SCANCODE_I,
SCANCODE_J = SDL_SCANCODE_J,
SCANCODE_K = SDL_SCANCODE_K,
SCANCODE_L = SDL_SCANCODE_L,
SCANCODE_M = SDL_SCANCODE_M,
SCANCODE_N = SDL_SCANCODE_N,
SCANCODE_O = SDL_SCANCODE_O,
SCANCODE_P = SDL_SCANCODE_P,
SCANCODE_Q = SDL_SCANCODE_Q,
SCANCODE_R = SDL_SCANCODE_R,
SCANCODE_S = SDL_SCANCODE_S,
SCANCODE_T = SDL_SCANCODE_T,
SCANCODE_U = SDL_SCANCODE_U,
SCANCODE_V = SDL_SCANCODE_V,
SCANCODE_W = SDL_SCANCODE_W,
SCANCODE_X = SDL_SCANCODE_X,
SCANCODE_Y = SDL_SCANCODE_Y,
SCANCODE_Z = SDL_SCANCODE_Z,
SCANCODE_1 = SDL_SCANCODE_1,
SCANCODE_2 = SDL_SCANCODE_2,
SCANCODE_3 = SDL_SCANCODE_3,
SCANCODE_4 = SDL_SCANCODE_4,
SCANCODE_5 = SDL_SCANCODE_5,
SCANCODE_6 = SDL_SCANCODE_6,
SCANCODE_7 = SDL_SCANCODE_7,
SCANCODE_8 = SDL_SCANCODE_8,
SCANCODE_9 = SDL_SCANCODE_9,
SCANCODE_0 = SDL_SCANCODE_0,
SCANCODE_RETURN = SDL_SCANCODE_RETURN,
SCANCODE_ESCAPE = SDL_SCANCODE_ESCAPE,
SCANCODE_BACKSPACE = SDL_SCANCODE_BACKSPACE,
SCANCODE_TAB = SDL_SCANCODE_TAB,
SCANCODE_SPACE = SDL_SCANCODE_SPACE,
SCANCODE_MINUS = SDL_SCANCODE_MINUS,
SCANCODE_EQUALS = SDL_SCANCODE_EQUALS,
SCANCODE_LEFTBRACKET = SDL_SCANCODE_LEFTBRACKET,
SCANCODE_RIGHTBRACKET = SDL_SCANCODE_RIGHTBRACKET,
SCANCODE_BACKSLASH = SDL_SCANCODE_BACKSLASH,
SCANCODE_NONUSHASH = SDL_SCANCODE_NONUSHASH,
SCANCODE_SEMICOLON = SDL_SCANCODE_SEMICOLON,
SCANCODE_APOSTROPHE = SDL_SCANCODE_APOSTROPHE,
SCANCODE_GRAVE = SDL_SCANCODE_GRAVE,
SCANCODE_COMMA = SDL_SCANCODE_COMMA,
SCANCODE_PERIOD = SDL_SCANCODE_PERIOD,
SCANCODE_SLASH = SDL_SCANCODE_SLASH,
SCANCODE_CAPSLOCK = SDL_SCANCODE_CAPSLOCK,
SCANCODE_F1 = SDL_SCANCODE_F1,
SCANCODE_F2 = SDL_SCANCODE_F2,
SCANCODE_F3 = SDL_SCANCODE_F3,
SCANCODE_F4 = SDL_SCANCODE_F4,
SCANCODE_F5 = SDL_SCANCODE_F5,
SCANCODE_F6 = SDL_SCANCODE_F6,
SCANCODE_F7 = SDL_SCANCODE_F7,
SCANCODE_F8 = SDL_SCANCODE_F8,
SCANCODE_F9 = SDL_SCANCODE_F9,
SCANCODE_F10 = SDL_SCANCODE_F10,
SCANCODE_F11 = SDL_SCANCODE_F11,
SCANCODE_F12 = SDL_SCANCODE_F12,
SCANCODE_PRINTSCREEN = SDL_SCANCODE_PRINTSCREEN,
SCANCODE_SCROLLLOCK = SDL_SCANCODE_SCROLLLOCK,
SCANCODE_PAUSE = SDL_SCANCODE_PAUSE,
SCANCODE_INSERT = SDL_SCANCODE_INSERT,
SCANCODE_HOME = SDL_SCANCODE_HOME,
SCANCODE_PAGEUP = SDL_SCANCODE_PAGEUP,
SCANCODE_DELETE = SDL_SCANCODE_DELETE,
SCANCODE_END = SDL_SCANCODE_END,
SCANCODE_PAGEDOWN = SDL_SCANCODE_PAGEDOWN,
SCANCODE_RIGHT = SDL_SCANCODE_RIGHT,
SCANCODE_LEFT = SDL_SCANCODE_LEFT,
SCANCODE_DOWN = SDL_SCANCODE_DOWN,
SCANCODE_UP = SDL_SCANCODE_UP,
SCANCODE_NUMLOCKCLEAR = SDL_SCANCODE_NUMLOCKCLEAR,
SCANCODE_KP_DIVIDE = SDL_SCANCODE_KP_DIVIDE,
SCANCODE_KP_MULTIPLY = SDL_SCANCODE_KP_MULTIPLY,
SCANCODE_KP_MINUS = SDL_SCANCODE_KP_MINUS,
SCANCODE_KP_PLUS = SDL_SCANCODE_KP_PLUS,
SCANCODE_KP_ENTER = SDL_SCANCODE_KP_ENTER,
SCANCODE_KP_1 = SDL_SCANCODE_KP_1,
SCANCODE_KP_2 = SDL_SCANCODE_KP_2,
SCANCODE_KP_3 = SDL_SCANCODE_KP_3,
SCANCODE_KP_4 = SDL_SCANCODE_KP_4,
SCANCODE_KP_5 = SDL_SCANCODE_KP_5,
SCANCODE_KP_6 = SDL_SCANCODE_KP_6,
SCANCODE_KP_7 = SDL_SCANCODE_KP_7,
SCANCODE_KP_8 = SDL_SCANCODE_KP_8,
SCANCODE_KP_9 = SDL_SCANCODE_KP_9,
SCANCODE_KP_0 = SDL_SCANCODE_KP_0,
SCANCODE_KP_PERIOD = SDL_SCANCODE_KP_PERIOD,
SCANCODE_NONUSBACKSLASH = SDL_SCANCODE_NONUSBACKSLASH,
SCANCODE_APPLICATION = SDL_SCANCODE_APPLICATION,
SCANCODE_POWER = SDL_SCANCODE_POWER,
SCANCODE_KP_EQUALS = SDL_SCANCODE_KP_EQUALS,
SCANCODE_F13 = SDL_SCANCODE_F13,
SCANCODE_F14 = SDL_SCANCODE_F14,
SCANCODE_F15 = SDL_SCANCODE_F15,
SCANCODE_F16 = SDL_SCANCODE_F16,
SCANCODE_F17 = SDL_SCANCODE_F17,
SCANCODE_F18 = SDL_SCANCODE_F18,
SCANCODE_F19 = SDL_SCANCODE_F19,
SCANCODE_F20 = SDL_SCANCODE_F20,
SCANCODE_F21 = SDL_SCANCODE_F21,
SCANCODE_F22 = SDL_SCANCODE_F22,
SCANCODE_F23 = SDL_SCANCODE_F23,
SCANCODE_F24 = SDL_SCANCODE_F24,
SCANCODE_EXECUTE = SDL_SCANCODE_EXECUTE,
SCANCODE_HELP = SDL_SCANCODE_HELP,
SCANCODE_MENU = SDL_SCANCODE_MENU,
SCANCODE_SELECT = SDL_SCANCODE_SELECT,
SCANCODE_STOP = SDL_SCANCODE_STOP,
SCANCODE_AGAIN = SDL_SCANCODE_AGAIN,
SCANCODE_UNDO = SDL_SCANCODE_UNDO,
SCANCODE_CUT = SDL_SCANCODE_CUT,
SCANCODE_COPY = SDL_SCANCODE_COPY,
SCANCODE_PASTE = SDL_SCANCODE_PASTE,
SCANCODE_FIND = SDL_SCANCODE_FIND,
SCANCODE_MUTE = SDL_SCANCODE_MUTE,
SCANCODE_VOLUMEUP = SDL_SCANCODE_VOLUMEUP,
SCANCODE_VOLUMEDOWN = SDL_SCANCODE_VOLUMEDOWN,
SCANCODE_KP_COMMA = SDL_SCANCODE_KP_COMMA,
SCANCODE_KP_EQUALSAS400 = SDL_SCANCODE_KP_EQUALSAS400,
SCANCODE_INTERNATIONAL1 = SDL_SCANCODE_INTERNATIONAL1,
SCANCODE_INTERNATIONAL2 = SDL_SCANCODE_INTERNATIONAL2,
SCANCODE_INTERNATIONAL3 = SDL_SCANCODE_INTERNATIONAL3,
SCANCODE_INTERNATIONAL4 = SDL_SCANCODE_INTERNATIONAL4,
SCANCODE_INTERNATIONAL5 = SDL_SCANCODE_INTERNATIONAL5,
SCANCODE_INTERNATIONAL6 = SDL_SCANCODE_INTERNATIONAL6,
SCANCODE_INTERNATIONAL7 = SDL_SCANCODE_INTERNATIONAL7,
SCANCODE_INTERNATIONAL8 = SDL_SCANCODE_INTERNATIONAL8,
SCANCODE_INTERNATIONAL9 = SDL_SCANCODE_INTERNATIONAL9,
SCANCODE_LANG1 = SDL_SCANCODE_LANG1,
SCANCODE_LANG2 = SDL_SCANCODE_LANG2,
SCANCODE_LANG3 = SDL_SCANCODE_LANG3,
SCANCODE_LANG4 = SDL_SCANCODE_LANG4,
SCANCODE_LANG5 = SDL_SCANCODE_LANG5,
SCANCODE_LANG6 = SDL_SCANCODE_LANG6,
SCANCODE_LANG7 = SDL_SCANCODE_LANG7,
SCANCODE_LANG8 = SDL_SCANCODE_LANG8,
SCANCODE_LANG9 = SDL_SCANCODE_LANG9,
SCANCODE_ALTERASE = SDL_SCANCODE_ALTERASE,
SCANCODE_SYSREQ = SDL_SCANCODE_SYSREQ,
SCANCODE_CANCEL = SDL_SCANCODE_CANCEL,
SCANCODE_CLEAR = SDL_SCANCODE_CLEAR,
SCANCODE_PRIOR = SDL_SCANCODE_PRIOR,
SCANCODE_RETURN2 = SDL_SCANCODE_RETURN2,
SCANCODE_SEPARATOR = SDL_SCANCODE_SEPARATOR,
SCANCODE_OUT = SDL_SCANCODE_OUT,
SCANCODE_OPER = SDL_SCANCODE_OPER,
SCANCODE_CLEARAGAIN = SDL_SCANCODE_CLEARAGAIN,
SCANCODE_CRSEL = SDL_SCANCODE_CRSEL,
SCANCODE_EXSEL = SDL_SCANCODE_EXSEL,
SCANCODE_KP_00 = SDL_SCANCODE_KP_00,
SCANCODE_KP_000 = SDL_SCANCODE_KP_000,
SCANCODE_THOUSANDSSEPARATOR = SDL_SCANCODE_THOUSANDSSEPARATOR,
SCANCODE_DECIMALSEPARATOR = SDL_SCANCODE_DECIMALSEPARATOR,
SCANCODE_CURRENCYUNIT = SDL_SCANCODE_CURRENCYUNIT,
SCANCODE_CURRENCYSUBUNIT = SDL_SCANCODE_CURRENCYSUBUNIT,
SCANCODE_KP_LEFTPAREN = SDL_SCANCODE_KP_LEFTPAREN,
SCANCODE_KP_RIGHTPAREN = SDL_SCANCODE_KP_RIGHTPAREN,
SCANCODE_KP_LEFTBRACE = SDL_SCANCODE_KP_LEFTBRACE,
SCANCODE_KP_RIGHTBRACE = SDL_SCANCODE_KP_RIGHTBRACE,
SCANCODE_KP_TAB = SDL_SCANCODE_KP_TAB,
SCANCODE_KP_BACKSPACE = SDL_SCANCODE_KP_BACKSPACE,
SCANCODE_KP_A = SDL_SCANCODE_KP_A,
SCANCODE_KP_B = SDL_SCANCODE_KP_B,
SCANCODE_KP_C = SDL_SCANCODE_KP_C,
SCANCODE_KP_D = SDL_SCANCODE_KP_D,
SCANCODE_KP_E = SDL_SCANCODE_KP_E,
SCANCODE_KP_F = SDL_SCANCODE_KP_F,
SCANCODE_KP_XOR = SDL_SCANCODE_KP_XOR,
SCANCODE_KP_POWER = SDL_SCANCODE_KP_POWER,
SCANCODE_KP_PERCENT = SDL_SCANCODE_KP_PERCENT,
SCANCODE_KP_LESS = SDL_SCANCODE_KP_LESS,
SCANCODE_KP_GREATER = SDL_SCANCODE_KP_GREATER,
SCANCODE_KP_AMPERSAND = SDL_SCANCODE_KP_AMPERSAND,
SCANCODE_KP_DBLAMPERSAND = SDL_SCANCODE_KP_DBLAMPERSAND,
SCANCODE_KP_VERTICALBAR = SDL_SCANCODE_KP_VERTICALBAR,
SCANCODE_KP_DBLVERTICALBAR = SDL_SCANCODE_KP_DBLVERTICALBAR,
SCANCODE_KP_COLON = SDL_SCANCODE_KP_COLON,
SCANCODE_KP_HASH = SDL_SCANCODE_KP_HASH,
SCANCODE_KP_SPACE = SDL_SCANCODE_KP_SPACE,
SCANCODE_KP_AT = SDL_SCANCODE_KP_AT,
SCANCODE_KP_EXCLAM = SDL_SCANCODE_KP_EXCLAM,
SCANCODE_KP_MEMSTORE = SDL_SCANCODE_KP_MEMSTORE,
SCANCODE_KP_MEMRECALL = SDL_SCANCODE_KP_MEMRECALL,
SCANCODE_KP_MEMCLEAR = SDL_SCANCODE_KP_MEMCLEAR,
SCANCODE_KP_MEMADD = SDL_SCANCODE_KP_MEMADD,
SCANCODE_KP_MEMSUBTRACT = SDL_SCANCODE_KP_MEMSUBTRACT,
SCANCODE_KP_MEMMULTIPLY = SDL_SCANCODE_KP_MEMMULTIPLY,
SCANCODE_KP_MEMDIVIDE = SDL_SCANCODE_KP_MEMDIVIDE,
SCANCODE_KP_PLUSMINUS = SDL_SCANCODE_KP_PLUSMINUS,
SCANCODE_KP_CLEAR = SDL_SCANCODE_KP_CLEAR,
SCANCODE_KP_CLEARENTRY = SDL_SCANCODE_KP_CLEARENTRY,
SCANCODE_KP_BINARY = SDL_SCANCODE_KP_BINARY,
SCANCODE_KP_OCTAL = SDL_SCANCODE_KP_OCTAL,
SCANCODE_KP_DECIMAL = SDL_SCANCODE_KP_DECIMAL,
SCANCODE_KP_HEXADECIMAL = SDL_SCANCODE_KP_HEXADECIMAL,
SCANCODE_LCTRL = SDL_SCANCODE_LCTRL,
SCANCODE_LSHIFT = SDL_SCANCODE_LSHIFT,
SCANCODE_LALT = SDL_SCANCODE_LALT,
SCANCODE_LGUI = SDL_SCANCODE_LGUI,
SCANCODE_RCTRL = SDL_SCANCODE_RCTRL,
SCANCODE_RSHIFT = SDL_SCANCODE_RSHIFT,
SCANCODE_RALT = SDL_SCANCODE_RALT,
SCANCODE_RGUI = SDL_SCANCODE_RGUI,
SCANCODE_MODE = SDL_SCANCODE_MODE,
SCANCODE_AUDIONEXT = SDL_SCANCODE_AUDIONEXT,
SCANCODE_AUDIOPREV = SDL_SCANCODE_AUDIOPREV,
SCANCODE_AUDIOSTOP = SDL_SCANCODE_AUDIOSTOP,
SCANCODE_AUDIOPLAY = SDL_SCANCODE_AUDIOPLAY,
SCANCODE_AUDIOMUTE = SDL_SCANCODE_AUDIOMUTE,
SCANCODE_MEDIASELECT = SDL_SCANCODE_MEDIASELECT,
SCANCODE_WWW = SDL_SCANCODE_WWW,
SCANCODE_MAIL = SDL_SCANCODE_MAIL,
SCANCODE_CALCULATOR = SDL_SCANCODE_CALCULATOR,
SCANCODE_COMPUTER = SDL_SCANCODE_COMPUTER,
SCANCODE_AC_SEARCH = SDL_SCANCODE_AC_SEARCH,
SCANCODE_AC_HOME = SDL_SCANCODE_AC_HOME,
SCANCODE_AC_BACK = SDL_SCANCODE_AC_BACK,
SCANCODE_AC_FORWARD = SDL_SCANCODE_AC_FORWARD,
SCANCODE_AC_STOP = SDL_SCANCODE_AC_STOP,
SCANCODE_AC_REFRESH = SDL_SCANCODE_AC_REFRESH,
SCANCODE_AC_BOOKMARKS = SDL_SCANCODE_AC_BOOKMARKS,
SCANCODE_BRIGHTNESSDOWN = SDL_SCANCODE_BRIGHTNESSDOWN,
SCANCODE_BRIGHTNESSUP = SDL_SCANCODE_BRIGHTNESSUP,
SCANCODE_DISPLAYSWITCH = SDL_SCANCODE_DISPLAYSWITCH,
SCANCODE_KBDILLUMTOGGLE = SDL_SCANCODE_KBDILLUMTOGGLE,
SCANCODE_KBDILLUMDOWN = SDL_SCANCODE_KBDILLUMDOWN,
SCANCODE_KBDILLUMUP = SDL_SCANCODE_KBDILLUMUP,
SCANCODE_EJECT = SDL_SCANCODE_EJECT,
SCANCODE_SLEEP = SDL_SCANCODE_SLEEP,
SCANCODE_APP1 = SDL_SCANCODE_APP1,
SCANCODE_APP2 = SDL_SCANCODE_APP2
}; };
void input_init(GLFWwindow* window); void input_init(void);
void input_cleanup(void); void input_cleanup(void);
int input_mousebutton_state_get(int button, int state_type); int input_mousebutton_state_get(uint button, int state_type);
int input_key_state_get(int key, int state_type); int input_key_state_get(int key, int state_type);
void input_cursor_pos_get(double* xpos, double* ypos); void input_mouse_pos_get(int* xpos, int* ypos);
void input_cursor_pos_set(double xpos, double ypos); void input_mouse_pos_set(int xpos, int ypos);
void input_cursor_mode_set(enum Cursor_Mode mode); void input_mouse_mode_set(enum Mouse_Mode mode);
int input_mouse_mode_get(void);
void input_update(void); void input_update(void);
int input_map_state_get(const char* map_name, int state); int input_map_state_get(const char* map_name, int state);
void input_map_create(const char* name, int* keys, size_t num_keys); void input_map_create(const char* name, int* keys, size_t num_keys);

@ -1,26 +1,25 @@
#define GLEW_STATIC
#include <GL/glew.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "log.h" #include "log.h"
#include "window_system.h" #include "platform.h"
#include "gl_load.h"
#include "game.h" #include "game.h"
const int WIN_WIDTH = 800; const int WIN_WIDTH = 1024;
const int WIN_HEIGHT = 600; const int WIN_HEIGHT = 768;
static struct Window* window = NULL;
int init(); int init();
void cleanup(); void cleanup();
int main(int argc, char** args) int main(int argc, char** args)
{ {
//Initialize window system and Glew
if(!init()) if(!init())
log_error("Main:main", "Could not initialize"); log_error("Main:main", "Could not initialize");
else else
game_init(); game_init(window);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -31,19 +30,30 @@ int init(void)
if(atexit(cleanup) != 0) if(atexit(cleanup) != 0)
{ {
success = 0; success = 0;
log_error("Main:init", "Could not register cleanup func with atexit"); log_error("main:init", "Could not register cleanup func with atexit");
} }
else else
{ {
if(window_init("Symmetry", WIN_WIDTH, WIN_HEIGHT)) if(platform_init())
{ {
//Initialize GLEW success = gl_load_library();
glewExperimental = GL_TRUE; if(!success)
GLenum glewError = glewInit();
if(glewError != GLEW_OK)
{ {
log_error("Main:init", "GLEW : %s", glewGetErrorString(glewError)); log_error("main:init", "Initializing OpenGL failed");
}
else
{
window = window_create("Symmetry", WIN_WIDTH, WIN_HEIGHT);
if(!window)
{
log_error("main:init", "Window creation failed");
success = 0; success = 0;
}
else
{
success = gl_load_extentions();
if(!success) log_error("main:init", "Failed to load opengl extentions");
}
} }
} }
else else
@ -57,6 +67,6 @@ int init(void)
void cleanup() void cleanup()
{ {
game_cleanup(); game_cleanup();
window_cleanup(); platform_cleanup();
log_message("Program exiting!"); log_message("Program exiting!");
} }

@ -10,9 +10,7 @@
#include "renderer.h" #include "renderer.h"
#include "material.h" #include "material.h"
#include "light.h" #include "light.h"
#include "gl_load.h"
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>

@ -0,0 +1,277 @@
#include "platform.h"
#include "log.h"
#include "input.h"
#include <SDL2/SDL.h>
//#define GL_DEBUG_CONTEXT
struct Window
{
void* sdl_window;
SDL_GLContext gl_context;
};
struct Platform_State
{
Keyboard_Event_Func on_keyboard_func;
Mousebutton_Event_Func on_mousebutton_func;
Mousemotion_Event_Func on_mousemotion_func;
Windowresize_Event_Func on_windowresize_func;
};
/* TODO: Find a better way to handle internal state */
static struct Platform_State* platform_state = NULL;
struct Window* window_create(const char* title, int width, int height)
{
struct Window* new_window = NULL;
if(!new_window)
{
new_window = malloc(sizeof(*new_window));
if(!new_window)
{
log_error("window_create", "Out of memory");
return NULL;
}
new_window->sdl_window = NULL;
new_window->gl_context = NULL;
}
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_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
#ifdef GL_DEBUG_CONTEXT
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif
SDL_Window* sdl_window = SDL_CreateWindow(title,
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height,
SDL_WINDOW_OPENGL |
SDL_WINDOW_RESIZABLE |
SDL_WINDOW_INPUT_FOCUS |
SDL_WINDOW_MOUSE_FOCUS);
if(!sdl_window)
{
log_error("window_create:SDL_CreateWindow", "Could not create window : %s", SDL_GetError());
free(new_window);
new_window = NULL;
return new_window;
}
new_window->sdl_window = sdl_window;
SDL_GLContext gl_context = SDL_GL_CreateContext(sdl_window);
if(!gl_context)
{
log_error("window_create:SDL_GL_CreateContext", "Failed to create GL context : %s", SDL_GetError());
window_destroy(new_window);
free(new_window);
new_window = NULL;
return new_window;
}
new_window->gl_context = gl_context;
SDL_Window* current_window = SDL_GL_GetCurrentWindow();
SDL_GLContext current_context = SDL_GL_GetCurrentContext();
SDL_GL_MakeCurrent((SDL_Window*)new_window->sdl_window, new_window->gl_context);
SDL_GL_SetSwapInterval(1); /* 0: Vsync disabled, 1: Vsync enabled*/
if(current_window && current_context) SDL_GL_MakeCurrent(current_window, current_context);
int major = 0, minor = 0;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
log_message("Window created and initialized with opengl core context %d.%d", major, minor);
return new_window;
}
void window_make_context_current(struct Window* window)
{
SDL_GL_MakeCurrent((SDL_Window*)window->sdl_window, window->gl_context);
}
void window_show(struct Window* window)
{
SDL_ShowWindow((SDL_Window*)window->sdl_window);
}
void window_hide(struct Window* window)
{
SDL_HideWindow((SDL_Window*)window->sdl_window);
}
void window_raise(struct Window* window)
{
SDL_RaiseWindow((SDL_Window*)window->sdl_window);
}
void window_destroy(struct Window* window)
{
if(window->gl_context) SDL_GL_DeleteContext(window->gl_context);
SDL_DestroyWindow((SDL_Window*)window->sdl_window);
}
void window_cleanup(void)
{
}
void window_set_size(struct Window* window, int width, int height)
{
SDL_SetWindowSize((SDL_Window*)window->sdl_window, width, height);
}
void window_get_size(struct Window* window, int* out_width, int* out_height)
{
SDL_GetWindowSize((SDL_Window*)window->sdl_window, out_width, out_height);
}
void window_swap_buffers(struct Window* window)
{
SDL_GL_SwapWindow(window->sdl_window);
}
int platform_init(void)
{
int success = 1;
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0)
{
success = 0;
log_error("platform_init", "SDL Init failed : %s", SDL_GetError());
}
else
{
platform_state = malloc(sizeof(*platform_state));
if(!platform_state)
{
log_error("platform_init", "Could not create platform state, out of memory");
success = 0;
}
}
return success;
}
void platform_cleanup(void)
{
if(platform_state) free(platform_state);
SDL_Quit();
}
void platform_poll_events(int* out_quit)
{
static SDL_Event event;
while(SDL_PollEvent(&event) != 0)
{
switch(event.type)
{
case SDL_QUIT:
*out_quit = 1;
break;
case SDL_KEYDOWN: case SDL_KEYUP:
{
int scancode = event.key.keysym.scancode;
int key = event.key.keysym.sym;
int state = event.key.state;
int mod_ctrl = (event.key.keysym.mod & KMOD_CTRL);
int mod_shift = (event.key.keysym.mod & KMOD_SHIFT);
platform_state->on_keyboard_func(key, scancode, state, mod_ctrl, mod_shift);
break;
}
case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP:
{
int button = event.button.button;
int state = event.button.state;
int num_clicks = event.button.clicks;
int x = event.button.x;
int y = event.button.y;
platform_state->on_mousebutton_func(button, state, x, y, num_clicks);
break;
}
case SDL_MOUSEMOTION:
{
int xrel = event.motion.xrel;
int yrel = event.motion.yrel;
int x = event.motion.x;
int y = event.motion.y;
platform_state->on_mousemotion_func(x, y, xrel, yrel);
}
case SDL_WINDOWEVENT:
{
if(event.window.type == SDL_WINDOWEVENT_RESIZED)
{
/* Notify renderer here */
platform_state->on_windowresize_func(event.window.data1, event.window.data2);
}
}
break;
}
}
}
void platform_keyboard_callback_set(Keyboard_Event_Func func)
{
platform_state->on_keyboard_func = func;
}
void platform_mousebutton_callback_set(Mousebutton_Event_Func func)
{
platform_state->on_mousebutton_func = func;
}
void platform_mousemotion_callback_set(Mousemotion_Event_Func func)
{
platform_state->on_mousemotion_func = func;
}
void platform_windowresize_callback_set(Windowresize_Event_Func func)
{
platform_state->on_windowresize_func = func;
}
int platform_key_state_get(int key)
{
/* Returns 1 if key is pressed, 0 otherwise */
const Uint8* keyboard_state = SDL_GetKeyboardState(NULL);
return keyboard_state[SDL_GetScancodeFromKey(key)];
}
int platform_mousebutton_state_get(uint button)
{
uint32 current_button_state = SDL_GetMouseState(NULL, NULL);
//return (current_button_state & button);
return current_button_state & button;
}
void platform_mouse_position_get(int* x, int* y)
{
SDL_GetMouseState(x, y);
}
void platform_mouse_position_set(struct Window* window, int x, int y)
{
SDL_WarpMouseInWindow(window->sdl_window, x, y);
}
void platform_mouse_global_position_set(int x, int y)
{
SDL_WarpMouseGlobal(x, y);
}
void platform_mouse_relative_mode_set(int relative_mode)
{
SDL_SetRelativeMouseMode(relative_mode ? SDL_TRUE : SDL_FALSE);
}
int platform_mouse_relative_mode_get(void)
{
return SDL_GetRelativeMouseMode() == SDL_TRUE ? 1 : 0;
}
uint32 platform_get_ticks(void)
{
return SDL_GetTicks();
}

@ -0,0 +1,43 @@
#ifndef PLATFORM_H
#define PLATFORM_H
#include "num_types.h"
// Function Pointer decls
typedef void (*Keyboard_Event_Func) (int key, int scancode, int state, int mod_ctrl, int mod_shift);
typedef void (*Mousebutton_Event_Func) (int button, int state, int x, int y, int8 num_clicks);
typedef void (*Mousemotion_Event_Func) (int x, int y, int xrel, int yrel);
typedef void (*Windowresize_Event_Func) (int x, int y);
// Window Related functions
struct Window;
struct Window* window_create(const char* title, int width, int height);
void window_destroy(struct Window* window);
void window_show(struct Window* window);
void window_hide(struct Window* window);
void window_raise(struct Window* window);
void window_make_context_current(struct Window* window);
void window_cleanup(void);
void window_set_size(struct Window* window, int width, int height);
void window_get_size(struct Window* window, int* out_width, int* out_height);
void window_swap_buffers(struct Window* window);
// Platform functions
int platform_init(void);
void platform_cleanup(void);
void platform_poll_events(int* out_quit);
void platform_keyboard_callback_set(Keyboard_Event_Func func);
void platform_mousebutton_callback_set(Mousebutton_Event_Func func);
void platform_mousemotion_callback_set(Mousemotion_Event_Func func);
void platform_windowresize_callback_set(Windowresize_Event_Func func);
int platform_key_state_get(int key);
int platform_mousebutton_state_get(uint button);
void platform_mouse_position_get(int* x, int* y);
void platform_mouse_position_set(struct Window* window, int x, int y);
void platform_mouse_global_position_set(int x, int y);
void platform_mouse_relative_mode_set(int relative_mode);
int platform_mouse_relative_mode_get(void);
uint32 platform_get_ticks(void);
#endif

@ -1,6 +1,5 @@
#include "renderer.h" #include "renderer.h"
#include "GL/glew.h" #include "gl_load.h"
#include "GLFW/glfw3.h"
#include "log.h" #include "log.h"
#include "camera.h" #include "camera.h"
@ -11,10 +10,10 @@
#include "geometry.h" #include "geometry.h"
#include "shader.h" #include "shader.h"
#include "num_types.h" #include "num_types.h"
#include "window_system.h"
#include "light.h" #include "light.h"
#include "entity.h" #include "entity.h"
#include "transform.h" #include "transform.h"
#include "game.h"
static int def_fbo = -1; static int def_fbo = -1;
static int def_albedo_tex = -1; static int def_albedo_tex = -1;
@ -23,16 +22,16 @@ static int quad_geo = -1;
static int composition_shader = -1; static int composition_shader = -1;
static struct Render_Settings settings; static struct Render_Settings settings;
void on_framebuffer_size_change(GLFWwindow* window, int width, int height); void on_framebuffer_size_change(int width, int height);
void renderer_init(GLFWwindow* window) void renderer_init(void)
{ {
glClearColor(0.3f, 0.6f, 0.9f, 1.0f); glClearColor(0.3f, 0.6f, 0.9f, 1.0f);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_BACK); glCullFace(GL_BACK);
glfwSetFramebufferSizeCallback(window, on_framebuffer_size_change); platform_windowresize_callback_set(on_framebuffer_size_change);
settings.fog.mode = FM_EXPONENTIAL; settings.fog.mode = FM_EXPONENTIAL;
settings.fog.density = 0.01f; settings.fog.density = 0.01f;
@ -72,7 +71,8 @@ void renderer_init(GLFWwindow* window)
array_free(indices); array_free(indices);
int width = -1, height = -1; int width = -1, height = -1;
window_get_size(&width, &height); struct Game_State* game_state = game_state_get();
window_get_size(game_state->window, &width, &height);
def_albedo_tex = texture_create("def_albedo_texture", def_albedo_tex = texture_create("def_albedo_texture",
TU_DIFFUSE, TU_DIFFUSE,
width, height, width, height,
@ -134,7 +134,8 @@ void renderer_draw(void)
} }
int width, height; int width, height;
window_get_size(&width, &height); struct Game_State* game_state = game_state_get();
window_get_size(game_state->window, &width, &height);
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader_bind(composition_shader); shader_bind(composition_shader);
@ -154,7 +155,7 @@ void renderer_cleanup(void)
texture_remove(def_depth_tex); texture_remove(def_depth_tex);
} }
void on_framebuffer_size_change(GLFWwindow* window, int width, int height) void on_framebuffer_size_change(int width, int height)
{ {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
struct Camera* camera = camera_get(0); struct Camera* camera = camera_get(0);

@ -27,11 +27,8 @@ struct Render_Settings
vec3 ambient_light; vec3 ambient_light;
}; };
typedef struct GLFWwindow GLFWwindow;
struct Render_Settings* renderer_get_settings(void); struct Render_Settings* renderer_get_settings(void);
void renderer_init(GLFWwindow* window); void renderer_init(void);
void renderer_draw(void); void renderer_draw(void);
void renderer_cleanup(void); void renderer_cleanup(void);
void renderer_set_clearcolor(float r, float g, float b, float a); void renderer_set_clearcolor(float r, float g, float b, float a);

@ -6,15 +6,13 @@
#include "log.h" #include "log.h"
#include "renderer.h" #include "renderer.h"
#include "texture.h" #include "texture.h"
#include "gl_load.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "GL/glew.h"
#include "GLFW/glfw3.h"
// Constants for locations of attributes inside all shaders // Constants for locations of attributes inside all shaders
const int POSITION_LOC = 0; const int POSITION_LOC = 0;
const int NORMAL_LOC = 1; const int NORMAL_LOC = 1;

@ -5,12 +5,11 @@
#include "log.h" #include "log.h"
#include "num_types.h" #include "num_types.h"
#include "renderer.h" #include "renderer.h"
#include "gl_load.h"
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "GL/glew.h"
#include "GLFW/glfw3.h"
struct Texture struct Texture
{ {

Loading…
Cancel
Save