commit df0491c6cbcf81ccadf5a0aad87dd5e507b797e9
parent 4e5302b5692130d24ae9d669dd633e386b491e90
Author: William Casarin <bill@casarin.me>
Date: Sun, 12 Apr 2015 12:53:29 -0700
A few changes
* GameState instead of global state
* Update compiler flags for better debugging
Diffstat:
14 files changed, 95 insertions(+), 71 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,7 +1,8 @@
NAME ?= polyadvent
BIN ?= $(NAME)
PREFIX ?= /usr/local
-LDFLAGS = -lSDL2 -lX11 -lGL -lXi -lXrandr -lXxf86vm
+CFLAGS = -ggdb -Wall -Wextra -Weverything -Werror
+LDFLAGS = -lSDL2 -lGL
DEFS= -DGLFW_INCLUDE_NONE
SRC=src
@@ -9,6 +10,7 @@ SHLIB=$(SRC)/lib$(NAME).so
OBJS = $(SRC)/window.o
OBJS += $(SRC)/buffer.o
+OBJS += $(SRC)/update.o
OBJS += $(SRC)/event.o
OBJS += $(SRC)/shader.o
OBJS += $(SRC)/file.o
@@ -18,13 +20,13 @@ OBJS += $(SRC)/render.o
all: $(BIN)
%.o: %.c %.h
- $(CC) -fPIC $(DEFS) -c $< -o $@
+ $(CC) $(CFLAGS) -fPIC $(DEFS) -c $< -o $@
$(SHLIB): $(OBJS)
- $(CC) -shared $^ -o $@
+ $(CC) $(CFLAGS) -shared $^ -o $@
$(BIN): $(SRC)/main.o $(SHLIB)
- $(CC) $(DEFS) $^ $(LDFLAGS) -o $@
+ $(CC) $(CFLAGS) $(DEFS) $^ $(LDFLAGS) -o $@
install: $(BIN)
install -d $(PREFIX)/bin
@@ -34,4 +36,4 @@ nixbuild:
nix-shell shell.nix --pure --command 'make -j4'
clean:
- rm -f $(OBJS) $(SHLIB) $(BIN)
+ rm -f main.o $(OBJS) $(SHLIB) $(BIN)
diff --git a/src/debug.h b/src/debug.h
@@ -4,8 +4,7 @@
#include "gl.h"
-void
-show_info_log();
+void show_info_log(void);
#endif /* POLYADVENT_DEBUG_H */
diff --git a/src/event.c b/src/event.c
@@ -4,7 +4,6 @@
void process_events() {
SDL_Event event;
- struct SDL_Window * window;
while (SDL_PollEvent(&event)) {
switch (event.type) {
@@ -14,7 +13,6 @@ void process_events() {
case SDL_QUIT:
SDL_Quit();
exit(0);
- break;
}
}
}
diff --git a/src/event.h b/src/event.h
@@ -4,6 +4,6 @@
#include "gl.h"
-void process_events();
+void process_events(void);
#endif /* PA_EVENT_H */
diff --git a/src/file.c b/src/file.c
@@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>
+#include "file.h"
void *
-file_contents(const char *filename, int *length) {
+file_contents(const char *filename, size_t *length) {
FILE *f = fopen(filename, "r");
void *buffer;
@@ -13,7 +14,7 @@ file_contents(const char *filename, int *length) {
}
fseek(f, 0, SEEK_END);
- *length = ftell(f);
+ *length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(*length+1);
diff --git a/src/file.h b/src/file.h
@@ -3,6 +3,6 @@
#include "gl.h"
-void *file_contents(const char *filename, GLint *length);
+void *file_contents(const char *filename, size_t *length);
#endif /* POLYADVEMT_FILE_H */
diff --git a/src/game.h b/src/game.h
@@ -2,8 +2,29 @@
#ifndef PA_GAME_H
#define PA_GAME_H
-typedef struct game_state {
+/*
+ * Global data used by our render callback:
+ */
+typedef struct TestResources {
+ GLuint vertex_buffer, element_buffer;
+ GLuint vertex_shader, fragment_shader, program;
+
+ struct {
+ GLint fade_factor;
+ } uniforms;
+
+ struct {
+ GLint position;
+ } attributes;
+
+ GLfloat fade_factor;
+} TestResources;
+
+
+typedef struct GameState {
int counter;
-} game_state;
+ TestResources test_resources;
+} GameState;
+
#endif /* PA_GAME_H */
diff --git a/src/main.c b/src/main.c
@@ -1,31 +1,34 @@
#include "gl.h"
+#include "game.h"
+#include "update.h"
#include "window.h"
#include "event.h"
#include "render.h"
int main(void)
{
- SDL_Event event;
+ GameState game;
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);
+ /* SDL_GLContext _SDL_GLContext _gl =gl = */ SDL_GL_CreateContext(window);
- init_gl();
+ init_gl(&game.test_resources);
/* Loop until the user closes the window */
while (1) {
process_events();
- render();
+ update(&game);
+ render(&game.test_resources);
/* Swap front and back buffers */
SDL_GL_SwapWindow(window);
}
- SDL_GL_DeleteContext(gl);
- return 0;
+ //SDL_GL_DeleteContext(gl);
+ //return 0;
}
diff --git a/src/render.c b/src/render.c
@@ -4,9 +4,11 @@
#include <stdlib.h>
#include "gl.h"
+#include "game.h"
#include "buffer.h"
#include "shader.h"
#include "debug.h"
+#include "render.h"
/*
* Global data used by our render callback:
@@ -20,98 +22,86 @@ static const GLfloat g_vertex_buffer_data[] = {
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() {
+init_gl(TestResources * resources) {
//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(
+ resources->vertex_buffer = make_buffer(
GL_ARRAY_BUFFER,
g_vertex_buffer_data,
sizeof(g_vertex_buffer_data)
);
- g_resources.element_buffer = make_buffer(
+ 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(
+ resources->vertex_shader = make_shader(
GL_VERTEX_SHADER,
SHADER("test.v.glsl")
);
- assert(g_resources.vertex_shader != 0);
- g_resources.fragment_shader = make_shader(
+ assert(resources->vertex_shader != 0);
+ resources->fragment_shader = make_shader(
GL_FRAGMENT_SHADER,
SHADER("test.f.glsl")
);
- assert(g_resources.fragment_shader != 0);
+ assert(resources->fragment_shader != 0);
// Shader program
- g_resources.program = make_program(g_resources.vertex_shader,
- g_resources.fragment_shader);
- assert(g_resources.program != 0);
+ resources->program = make_program(resources->vertex_shader,
+ resources->fragment_shader);
+ assert(resources->program != 0);
// Program variables
- g_resources.uniforms.fade_factor
- = glGetUniformLocation(g_resources.program, "fade_factor");
+ resources->uniforms.fade_factor
+ = glGetUniformLocation(resources->program, "fade_factor");
- g_resources.attributes.position
- = glGetAttribLocation(g_resources.program, "position");
+ resources->attributes.position
+ = glGetAttribLocation(resources->program, "position");
- assert(g_resources.program != 0);
+ assert(resources->program != 0);
}
void
-render () {
+render (TestResources * resources) {
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);
+ glUseProgram(resources->program);
+
+ /* glUniform1f(resources->uniforms.fade_factor, */
+ /* resources->fade_factor) */;
+
+ glBindBuffer(GL_ARRAY_BUFFER,
+ resources->vertex_buffer);
- glBindBuffer(GL_ARRAY_BUFFER, g_resources.vertex_buffer);
glVertexAttribPointer(
- g_resources.attributes.position, /* attribute */
+ (GLuint)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);
+ glEnableVertexAttribArray((GLuint)resources->attributes.position);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,
+ resources->element_buffer);
+
glDrawElements(
GL_TRIANGLE_STRIP, /* mode */
4, /* count */
@@ -119,5 +109,5 @@ render () {
(void*)0 /* element array buffer offset */
);
- glDisableVertexAttribArray(g_resources.attributes.position);
+ glDisableVertexAttribArray((GLuint)resources->attributes.position);
}
diff --git a/src/render.h b/src/render.h
@@ -1,7 +1,7 @@
#ifndef POLYADVENT_RENDER_H
#define POLYADVENT_RENDER_H
-void init_gl();
-void render();
+void init_gl(TestResources * resources);
+void render(TestResources * resources);
#endif /* POLYADVENT_RENDER_H */
diff --git a/src/shader.c b/src/shader.c
@@ -1,13 +1,15 @@
#include <stdlib.h>
#include <stdio.h>
+
#include "file.h"
#include "gl.h"
#include "debug.h"
+#include "shader.h"
GLuint
make_shader(GLenum type, const char *filename) {
- GLint length;
+ size_t length;
GLchar *source = (GLchar *)file_contents(filename, &length);
GLuint shader;
GLint shader_ok;
@@ -16,7 +18,7 @@ make_shader(GLenum type, const char *filename) {
return 0;
shader = glCreateShader(type);
- glShaderSource(shader, 1, (const GLchar**)&source, &length);
+ glShaderSource(shader, 1, (const GLchar**)&source, (GLint*)&length);
free(source);
glCompileShader(shader);
@@ -24,7 +26,7 @@ make_shader(GLenum type, const char *filename) {
if (!shader_ok) {
fprintf(stderr, "Failed to compile %s:\n", filename);
- show_info_log(shader, glGetShaderiv, glGetShaderInfoLog);
+ //show_info_log(shader, glGetShaderiv, glGetShaderInfoLog);
glDeleteShader(shader);
return 0;
}
@@ -48,7 +50,7 @@ make_program(GLuint vertex_shader, GLuint fragment_shader) {
glGetProgramiv(program, GL_LINK_STATUS, &program_ok);
if (!program_ok) {
fprintf(stderr, "Failed to link shader program:\n");
- show_info_log(program, glGetProgramiv, glGetProgramInfoLog);
+ //show_info_log(program, glGetProgramiv, glGetProgramInfoLog);
glDeleteProgram(program);
return 0;
}
diff --git a/src/update.c b/src/update.c
@@ -1,3 +1,10 @@
-void update () {
+#include "gl.h"
+#include "update.h"
+
+void update (GameState * game) {
+ unsigned int ms = SDL_GetTicks();
+
+ // Update fade effect in shader
+ game->test_resources.fade_factor = sinf((float)ms * 0.001f) * 0.5f + 0.5f;
}
diff --git a/src/update.h b/src/update.h
@@ -4,6 +4,6 @@
#include "game.h"
-void update(game_state * game);
+void update(GameState * game);
#endif /* PA_UPDATE_H */
diff --git a/src/window.c b/src/window.c
@@ -1,6 +1,7 @@
#include "gl.h"
+#include "window.h"
void
handle_resize(int width, int height) {