polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit ef9188a755dc270fa27997de5144a0825699f318
parent 3ec43ba51f6ca9ca04799e0851ddf9c5e7648e68
Author: William Casarin <bill@casarin.me>
Date:   Wed,  8 Apr 2015 01:31:23 -0700

switch to c and sdl2

Diffstat:
M.gitignore | 5+++--
MMakefile | 12++++++------
Mdefault.nix | 9+++++++--
Rsrc/buffer.cc -> src/buffer.c | 0
Asrc/debug.c | 8++++++++
Dsrc/debug.cc | 21---------------------
Msrc/debug.h | 6+-----
Rsrc/delaunay.cc -> src/delaunay.c | 0
Asrc/event.c | 20++++++++++++++++++++
Asrc/event.h | 9+++++++++
Rsrc/file.cc -> src/file.c | 0
Msrc/gl.h | 5+++--
Asrc/main.c | 31+++++++++++++++++++++++++++++++
Dsrc/main.cc | 48------------------------------------------------
Asrc/render.c | 123+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/render.cc | 115-------------------------------------------------------------------------------
Rsrc/shader.cc -> src/shader.c | 0
Rsrc/update.cc -> src/update.c | 0
Asrc/window.c | 12++++++++++++
Dsrc/window.cc | 12------------
Msrc/window.h | 5++++-
21 files changed, 227 insertions(+), 214 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1 +1,3 @@ -*.o- \ No newline at end of file +*.o +*.so +polyadvent diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ NAME ?= polyadvent BIN ?= $(NAME) PREFIX ?= /usr/local -LDFLAGS = -lglfw3 -lX11 -lGL -lXi -lXrandr -lXxf86vm -lepoxy +LDFLAGS = -lSDL2 -lX11 -lGL -lXi -lXrandr -lXxf86vm DEFS= -DGLFW_INCLUDE_NONE SRC=src @@ -9,6 +9,7 @@ SHLIB=$(SRC)/lib$(NAME).so OBJS = $(SRC)/window.o OBJS += $(SRC)/buffer.o +OBJS += $(SRC)/event.o OBJS += $(SRC)/shader.o OBJS += $(SRC)/file.o OBJS += $(SRC)/debug.o @@ -16,13 +17,13 @@ OBJS += $(SRC)/render.o all: $(BIN) -%.o: %.cc %.h +%.o: %.c %.h $(CC) -fPIC $(DEFS) -c $< -o $@ $(SHLIB): $(OBJS) $(CC) -shared $^ -o $@ -$(BIN): $(SHLIB) $(SRC)/main.o +$(BIN): $(SRC)/main.o $(SHLIB) $(CC) $(DEFS) $^ $(LDFLAGS) -o $@ install: $(BIN) @@ -30,8 +31,7 @@ install: $(BIN) install $(BIN) $(PREFIX)/bin nixbuild: - nix-shell shell.nix --command 'make -j4' + nix-shell shell.nix --pure --command 'make -j4' clean: - rm -rf $(OBJS) $(SHLIB) $(BIN) - + rm -f $(OBJS) $(SHLIB) $(BIN) diff --git a/default.nix b/default.nix @@ -1,4 +1,9 @@ -{clangStdenv, glfw, mesa, xlibs, epoxy}: +{ clangStdenv +, SDL2 +, mesa +, epoxy +, xlibs +}: let stdenv = clangStdenv; @@ -10,7 +15,7 @@ in stdenv.mkDerivation rec { makeFlags = "PREFIX=$(out)"; - buildInputs = [ glfw mesa epoxy ] + buildInputs = [ SDL2 mesa epoxy ] ++ (with xlibs; [ libX11 libXi libXrandr libXxf86vm ] ); meta = with stdenv.lib; { diff --git a/src/buffer.cc b/src/buffer.c diff --git a/src/debug.c b/src/debug.c @@ -0,0 +1,8 @@ + +#include "debug.h" +#include <stdlib.h> +#include <stdio.h> + +void +show_info_log() { +} diff --git a/src/debug.cc b/src/debug.cc @@ -1,21 +0,0 @@ - -#include "debug.h" -#include <stdlib.h> -#include <stdio.h> - -void -show_info_log( - GLuint object, - PFNGLGETSHADERIVPROC glGet__iv, - PFNGLGETSHADERINFOLOGPROC glGet__InfoLog -) { - GLint log_length; - char *log; - - glGet__iv(object, GL_INFO_LOG_LENGTH, &log_length); - // TODO (jb55): remove mallocs? - log = (char*)malloc(log_length); - glGet__InfoLog(object, log_length, NULL, log); - fprintf(stderr, "%s", log); - free(log); -} diff --git a/src/debug.h b/src/debug.h @@ -5,11 +5,7 @@ #include "gl.h" void -show_info_log( - GLuint object, - PFNGLGETSHADERIVPROC glGet__iv, - PFNGLGETSHADERINFOLOGPROC glGet__InfoLog -); +show_info_log(); #endif /* POLYADVENT_DEBUG_H */ diff --git a/src/delaunay.cc b/src/delaunay.c diff --git a/src/event.c b/src/event.c @@ -0,0 +1,20 @@ + +#include "event.h" +#include "window.h" + +void process_events() { + SDL_Event event; + struct SDL_Window * window; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_WINDOWEVENT_RESIZED: + handle_resize(event.window.data1, event.window.data2); + break; + case SDL_QUIT: + SDL_Quit(); + exit(0); + break; + } + } +} diff --git a/src/event.h b/src/event.h @@ -0,0 +1,9 @@ + +#ifndef PA_EVENT_H +#define PA_EVENT_H + +#include "gl.h" + +void process_events(); + +#endif /* PA_EVENT_H */ diff --git a/src/file.cc b/src/file.c diff --git a/src/gl.h b/src/gl.h @@ -1,7 +1,8 @@ #ifndef POLYADVENT_GL_H #define POLYADVENT_GL_H -#include <epoxy/gl.h> -#include <GLFW/glfw3.h> +//#include <epoxy/gl.h> +#include <SDL2/SDL.h> +#include <SDL2/SDL_opengles2.h> #endif /* POLYADVENT_GL_H */ diff --git a/src/main.c b/src/main.c @@ -0,0 +1,31 @@ + +#include "gl.h" +#include "window.h" +#include "event.h" +#include "render.h" + +int main(void) +{ + SDL_Event event; + + SDL_Window *window = SDL_CreateWindow( + "SDL2/OpenGL Demo", 0, 0, 640, 480, + SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE); + + SDL_GLContext gl = SDL_GL_CreateContext(window); + + init_gl(); + + /* Loop until the user closes the window */ + while (1) { + process_events(); + render(); + + /* Swap front and back buffers */ + SDL_GL_SwapWindow(window); + } + + SDL_GL_DeleteContext(gl); + return 0; +} + diff --git a/src/main.cc b/src/main.cc @@ -1,48 +0,0 @@ - -#include "gl.h" -#include "window.h" -#include "render.h" - -int main(void) -{ - GLFWwindow* window; - - /* Initialize the library */ - if (!glfwInit()) - return -1; - - /* Create a windowed mode window and its OpenGL context */ - window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); - if (!window) { - glfwTerminate(); - return -1; - } - - /* Make the window's context current */ - glfwMakeContextCurrent(window); - glfwSetWindowSizeCallback(window, handle_resize); - - init_gl(); - - /* Loop until the user closes the window */ - while (!glfwWindowShouldClose(window)) { - glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); //clear background screen to black - glClear( GL_COLOR_BUFFER_BIT ); - - glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective - glLoadIdentity(); //Reset the drawing perspective - - //update(); - render(); - - /* Swap front and back buffers */ - glfwSwapBuffers(window); - - /* Poll for and process events */ - glfwPollEvents(); - } - - glfwTerminate(); - return 0; -} - diff --git a/src/render.c b/src/render.c @@ -0,0 +1,123 @@ + +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +#include "gl.h" +#include "buffer.h" +#include "shader.h" +#include "debug.h" + +/* + * Global data used by our render callback: + */ +static const GLfloat g_vertex_buffer_data[] = { + -0.5f, -0.5f, + 0.5f, -0.5f, + -0.5f, 0.6f, + 0.5f, 0.5f +}; +static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 }; + + +/* + * Global data used by our render callback: + */ +static struct { + GLuint vertex_buffer, element_buffer; + GLuint textures[2]; + GLuint vertex_shader, fragment_shader, program; + + struct { + GLint fade_factor; + GLint textures[2]; + } uniforms; + + struct { + GLint position; + } attributes; + + GLfloat fade_factor; +} g_resources; + + + +void +init_gl() { + //glEnable(GL_DEPTH_TEST); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); + + // VBOs + g_resources.vertex_buffer = make_buffer( + GL_ARRAY_BUFFER, + g_vertex_buffer_data, + sizeof(g_vertex_buffer_data) + ); + g_resources.element_buffer = make_buffer( + GL_ELEMENT_ARRAY_BUFFER, + g_element_buffer_data, + sizeof(g_element_buffer_data) + ); + + // Shaders + g_resources.vertex_shader = make_shader( + GL_VERTEX_SHADER, + SHADER("test.v.glsl") + ); + assert(g_resources.vertex_shader != 0); + g_resources.fragment_shader = make_shader( + GL_FRAGMENT_SHADER, + SHADER("test.f.glsl") + ); + assert(g_resources.fragment_shader != 0); + + + // Shader program + g_resources.program = make_program(g_resources.vertex_shader, + g_resources.fragment_shader); + assert(g_resources.program != 0); + + + // Program variables + g_resources.uniforms.fade_factor + = glGetUniformLocation(g_resources.program, "fade_factor"); + + g_resources.attributes.position + = glGetAttribLocation(g_resources.program, "position"); + + assert(g_resources.program != 0); +} + + +void +render () { + glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); //clear background screen to black + glClear( GL_COLOR_BUFFER_BIT ); + + //glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective + //glLoadIdentity(); //Reset the drawing perspective + + glUseProgram(g_resources.program); + + glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer); + glVertexAttribPointer( + g_resources.attributes.position, /* attribute */ + 2, /* size */ + GL_FLOAT, /* type */ + GL_FALSE, /* normalized? */ + sizeof(GLfloat)*2, /* stride */ + (void*)0 /* array buffer offset */ + ); + glEnableVertexAttribArray(g_resources.attributes.position); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer); + glDrawElements( + GL_TRIANGLE_STRIP, /* mode */ + 4, /* count */ + GL_UNSIGNED_SHORT, /* type */ + (void*)0 /* element array buffer offset */ + ); + + glDisableVertexAttribArray(g_resources.attributes.position); +} diff --git a/src/render.cc b/src/render.cc @@ -1,115 +0,0 @@ - -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> - -#include "gl.h" -#include "buffer.h" -#include "shader.h" -#include "debug.h" - -/* - * Global data used by our render callback: - */ -static const GLfloat g_vertex_buffer_data[] = { - -0.5f, -0.5f, - 0.5f, -0.5f, - -0.5f, 0.6f, - 0.5f, 0.5f -}; -static const GLushort g_element_buffer_data[] = { 0, 1, 2, 3 }; - - -/* - * Global data used by our render callback: - */ -static struct { - GLuint vertex_buffer, element_buffer; - GLuint textures[2]; - GLuint vertex_shader, fragment_shader, program; - - struct { - GLint fade_factor; - GLint textures[2]; - } uniforms; - - struct { - GLint position; - } attributes; - - GLfloat fade_factor; -} g_resources; - - - -void -init_gl() { - //glEnable(GL_DEPTH_TEST); - - // VBOs - g_resources.vertex_buffer = make_buffer( - GL_ARRAY_BUFFER, - g_vertex_buffer_data, - sizeof(g_vertex_buffer_data) - ); - g_resources.element_buffer = make_buffer( - GL_ELEMENT_ARRAY_BUFFER, - g_element_buffer_data, - sizeof(g_element_buffer_data) - ); - - // Shaders - g_resources.vertex_shader = make_shader( - GL_VERTEX_SHADER, - SHADER("test.v.glsl") - ); - assert(g_resources.vertex_shader != 0); - g_resources.fragment_shader = make_shader( - GL_FRAGMENT_SHADER, - SHADER("test.f.glsl") - ); - assert(g_resources.fragment_shader != 0); - - - // Shader program - g_resources.program = make_program(g_resources.vertex_shader, - g_resources.fragment_shader); - assert(g_resources.program != 0); - - - // Program variables - g_resources.uniforms.fade_factor - = glGetUniformLocation(g_resources.program, "fade_factor"); - - g_resources.attributes.position - = glGetAttribLocation(g_resources.program, "position"); - - assert(g_resources.program != 0); -} - - -void -render () { - glUseProgram(g_resources.program); - - glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer); - glVertexAttribPointer( - g_resources.attributes.position, /* attribute */ - 2, /* size */ - GL_FLOAT, /* type */ - GL_FALSE, /* normalized? */ - sizeof(GLfloat)*2, /* stride */ - (void*)0 /* array buffer offset */ - ); - glEnableVertexAttribArray(g_resources.attributes.position); - - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_resources.element_buffer); - glDrawElements( - GL_TRIANGLE_STRIP, /* mode */ - 4, /* count */ - GL_UNSIGNED_SHORT, /* type */ - (void*)0 /* element array buffer offset */ - ); - - glDisableVertexAttribArray(g_resources.attributes.position); -} diff --git a/src/shader.cc b/src/shader.c diff --git a/src/update.cc b/src/update.c diff --git a/src/window.c b/src/window.c @@ -0,0 +1,12 @@ + + +#include "gl.h" + +void +handle_resize(int width, int height) { + glViewport( 0, 0, width, height ); + + //glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective + //Set the camera perspective + //glLoadIdentity(); //reset the camera +} diff --git a/src/window.cc b/src/window.cc @@ -1,12 +0,0 @@ - - -#include "gl.h" - -void -handle_resize(GLFWwindow * window, int width, int height) { - glViewport( 0, 0, width, height ); - - glMatrixMode( GL_PROJECTION ); //Switch to setting the camera perspective - //Set the camera perspective - glLoadIdentity(); //reset the camera -} diff --git a/src/window.h b/src/window.h @@ -2,7 +2,10 @@ #ifndef POLYADVENT_WINDOW_H #define POLYADVENT_WINDOW_H -void handle_resize(GLFWwindow * window, int width, int height); +#include "gl.h" + +void +handle_resize(int width, int height); #endif /* POLYADVENT_WINDOW_H */