Skip to content

Commit

Permalink
initial SDL2 implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
exezin committed Aug 20, 2019
1 parent ad58ba0 commit 22da048
Show file tree
Hide file tree
Showing 123 changed files with 36,371 additions and 28,141 deletions.
14 changes: 5 additions & 9 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

# -- Dirs -- #
CC =gcc
CPP =g++
EDIR =exengine
LDIR =lib
BDIR =build
Expand All @@ -12,25 +11,22 @@ _IDIRS =lib inc lib/physfs
IDIRS =$(patsubst %,-I%/,$(_IDIRS))

# -- Flags -- #
FLAGS =-g -lm -ldl -Wall -Wno-unused -Wno-uninitialized -lstdc++ -lGL -lglfw -lopenal -I. $(IDIRS) '-Wl,-z,origin' '-Wl,-rpath,$$ORIGIN/lib'
FLAGS =-g -lm -ldl -Wall -Wno-unused -Wno-uninitialized -lstdc++ -lGL -lSDL2 -lglfw -lopenal -I. $(IDIRS) '-Wl,-z,origin' '-Wl,-rpath,$$ORIGIN/lib'
CFLAGS =$(FLAGS)
CFLAGS +=-std=c99 -O2
CPPFLAGS=

# -- Windows -- #
ifeq ($(OS),Windows_NT)
CC =x86_64-w64-mingw32-gcc
CPP =x86_64-w64-mingw32-g++
FLAGS =-g -lm -static -static-libgcc -static-libstdc++ -lstdc++ -Llib/win -lopengl32 -lglfw3dll -lopenal32 -I. $(IDIRS)
FLAGS =-g -lm -static -static-libgcc -static-libstdc++ -lstdc++ -Llib/win -lopengl32 -lSDL2 -lglfw3dll -lopenal32 -I. $(IDIRS)
CFLAGS =$(FLAGS)
CFLAGS +=-std=c99
CPPFLAGS=-lstdc++
endif

# -- MacOS -- #
UNAME = $(shell uname -s)
ifeq ($(UNAME),Darwin)
FLAGS =-g -lstdc++ -framework OpenGl -framework Foundation -framework IOKit -lglfw -lphysfs -framework OpenAL -I. $(IDIRS) -Wno-unused-command-line-argument
FLAGS =-g -lstdc++ -framework OpenGl -framework Foundation -framework IOKit -lglfw -lSDL2 -lphysfs -framework OpenAL -I. $(IDIRS) -Wno-unused-command-line-argument
CFLAGS =$(FLAGS)
CFLAGS +=-std=c99 -O2
endif
Expand All @@ -39,7 +35,6 @@ endif
UNAME = $(shell uname -s)
ifeq ($(UNAME),OpenBSD)
CC =egcc
CPP =eg++
FLAGS += -I/usr/X11R6/include -L/usr/X11R6/lib
endif

Expand Down Expand Up @@ -123,9 +118,10 @@ endif

release-linux:
mkdir -p $(BDIR)/lib
rm $(BDIR)/lib/*
rm -f $(BDIR)/lib/*
cp $$(ldd $(BDIR)/game | grep -i libglfw | awk 'NF == 4 {print $$3}; NF == 2 {print $$1}') $(BDIR)/lib
cp $$(ldd $(BDIR)/game | grep -i libopenal | awk 'NF == 4 {print $$3}; NF == 2 {print $$1}') $(BDIR)/lib
cp $$(ldd $(BDIR)/game | grep -i libsdl2 | awk 'NF == 4 {print $$3}; NF == 2 {print $$1}') $(BDIR)/lib

#release:
#ifeq ($(OS),Windows_NT)
Expand Down
2 changes: 1 addition & 1 deletion src/exengine/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define EX_CACHE_H

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>

#include "model.h"

Expand Down
6 changes: 2 additions & 4 deletions src/exengine/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ void ex_fps_camera_update(ex_fps_camera_t *cam)
float x = display.mouse_x;
float y = display.mouse_y;

float offset_x = x - cam->last_x;
float offset_y = cam->last_y - y;
cam->last_x = x;
cam->last_y = y;
float offset_x = x;
float offset_y = -y;

offset_x *= cam->sensitivity;
offset_y *= cam->sensitivity;
Expand Down
52 changes: 41 additions & 11 deletions src/exengine/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "defaults.h"
#include "text.h"
#include "cache.h"
#include "dbgui.h"
#include "input.h"

// renderer feature toggles
int ex_enable_ssao = 1;
Expand All @@ -23,7 +23,7 @@ void (*ex_update_ptr)(double);
void (*ex_draw_ptr)(void);
void (*ex_exit_ptr)(void);
// non-essential user callbacks
void (*ex_keypressed_ptr)(int, int, int, int);
void (*ex_keypressed_ptr)(int);
void (*ex_mousepressed_ptr)(int, int, int);
void (*ex_keyinput_ptr)(unsigned int);
void (*ex_mousescroll_ptr)(double, double);
Expand Down Expand Up @@ -77,14 +77,13 @@ void exengine(char **argv, uint8_t flags)

/* -- UPDATE ENGINE -- */
// main engine loop
double last_ex_frame_time = glfwGetTime();
while (!glfwWindowShouldClose(display.window)) {
// handle window events
ex_window_begin();

double last_ex_frame_time = SDL_GetPerformanceCounter();
int running = 1;
SDL_Event e;
while (running) {
// calculate delta time
double current_ex_frame_time = (double)glfwGetTime();
delta_time = current_ex_frame_time - last_ex_frame_time;
double current_ex_frame_time = (double)SDL_GetPerformanceCounter();
delta_time = (double)(current_ex_frame_time - last_ex_frame_time) / (double)SDL_GetPerformanceFrequency();
last_ex_frame_time = current_ex_frame_time;

// prevent spiral of death
Expand All @@ -94,7 +93,38 @@ void exengine(char **argv, uint8_t flags)
// update at a constant rate to keep physics in check
accumulator += delta_time;
while (accumulator >= phys_delta_time) {
glfwPollEvents();
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = 0;
break;
case SDL_KEYDOWN: {
ex_keys_down[event.key.keysym.scancode] = 1;
if (event.key.repeat == 0)
ex_keypressed_ptr(event.key.keysym.scancode);
break;
}
case SDL_KEYUP: {
ex_keys_down[event.key.keysym.scancode] = 0;
break;
}
case SDL_MOUSEBUTTONDOWN: {
ex_buttons_down[event.button.button] = 1;
break;
}
case SDL_MOUSEBUTTONUP: {
ex_buttons_down[event.button.button] = 0;
break;
}
}
}

// update mouse coords
int mx, my;
SDL_GetRelativeMouseState(&mx, &my);
display.mouse_x = (float)mx;
display.mouse_y = (float)my;

// user update callback
ex_update_ptr(phys_delta_time);
Expand All @@ -108,7 +138,7 @@ void exengine(char **argv, uint8_t flags)

// swap buffers render gui etc
ex_window_end();
glfwSwapBuffers(display.window);
SDL_GL_SwapWindow(display.window);
}
/* ------------------- */

Expand Down
4 changes: 2 additions & 2 deletions src/exengine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define EX_DATA_FILE "data.ex"

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>

#include <inttypes.h>
#include <stdbool.h>
Expand All @@ -48,7 +48,7 @@ extern void (*ex_init_ptr)(void);
extern void (*ex_update_ptr)(double);
extern void (*ex_draw_ptr)(void);
extern void (*ex_exit_ptr)(void);
extern void (*ex_keypressed_ptr)(int, int, int, int);
extern void (*ex_keypressed_ptr)(int);
extern void (*ex_mousepressed_ptr)(int, int, int);
extern void (*ex_keyinput_ptr)(unsigned int);
extern void (*ex_mousescroll_ptr)(double, double);
Expand Down
2 changes: 1 addition & 1 deletion src/exengine/framebuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ ex_framebuffer_t* ex_framebuffer_new(int width, int height)
fb->height = height;

int vw, vh;
glfwGetFramebufferSize(display.window, &vw, &vh);
SDL_GetWindowSize(display.window, &vw, &vh);
if (!width)
fb->width = vw;
if (!height)
Expand Down
2 changes: 1 addition & 1 deletion src/exengine/framebuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define EX_FRAMEBUFFER_H

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>

extern GLuint ex_fbo_shader;

Expand Down
2 changes: 1 addition & 1 deletion src/exengine/gbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void ex_gbuffer_init(int reinit)
glGenFramebuffers(1, &gbuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gbuffer);

glfwGetFramebufferSize(display.window, &width, &height);
SDL_GetWindowSize(display.window, &width, &height);

// position buffer
glGenTextures(1, &gposition);
Expand Down
2 changes: 1 addition & 1 deletion src/exengine/gbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#define EX_GBUFFER_H

#include "glad/glad.h"
#include <GLFW/glfw3.h>
#include <SDL2/SDL.h>

extern GLuint ex_gshader, ex_gmainshader;
extern GLuint gposition, gnormal;
Expand Down
Loading

0 comments on commit 22da048

Please sign in to comment.