Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
450 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ | |
*.swp | ||
*.swo | ||
/tmp/* | ||
*.o | ||
*.so |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
SRC=src/*.c \ | ||
src/resources/*.c | ||
BIN=uo | ||
CFLAGS=-std=c99 -Wall -g -fPIC -pedantic -O2 | ||
LDLIBS=-lglfw3 -ldl -framework OpenGL | ||
SRC_MAIN=src/main.c | ||
SRC_GAME=src/game/*.c \ | ||
src/game/core/*.c | ||
|
||
all: | ||
mkdir -p bin | ||
gcc $(SRC) -g -L ../raylib/release/osx -I ../raylib/src -lglfw3 -lraylib -framework OpenGL -o bin/${BIN} | ||
all: game libgame.so | ||
|
||
run: all | ||
./bin/${BIN} | ||
game: | ||
$(CC) $(SRC_MAIN) $(CFLAGS) $(LDFLAGS) -o bin/$@ $(LDLIBS) | ||
|
||
libgame.so: $(SRC_GAME) | ||
$(CC) $(CFLAGS) -shared $(LDFLAGS) -o bin/$@ $(SRC_GAME) $(LDLIBS) | ||
|
||
clean: | ||
$(RM) bin/* | ||
|
||
run: game libgame.so | ||
./bin/$< |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include "drawing.h" | ||
|
||
void begin_drawing(struct Window * window) | ||
{ | ||
} | ||
|
||
void end_drawing(struct Window * window) | ||
{ | ||
glfwSwapBuffers(window->handle); | ||
} | ||
|
||
void clear_background(Color color) | ||
{ | ||
glClearColor(color.red, color.green, color.blue, color.alpha); | ||
glClear(GL_COLOR_BUFFER_BIT); | ||
} | ||
|
||
void draw_quad(Color color) | ||
{ | ||
glBegin(GL_QUADS); //Begin quadrilateral coordinates | ||
|
||
glColor4ub(color.red, color.green, color.blue, color.alpha); | ||
|
||
glVertex3f(-0.5f, 0.5f, 0.0f); | ||
glVertex3f( 0.5f, 0.5f, 0.0f); | ||
glVertex3f( 0.5f,-0.5f, 0.0f); | ||
glVertex3f(-0.5f,-0.5f, 0.0f); | ||
|
||
glEnd(); | ||
} | ||
|
||
void begin_3d(struct Window * window) | ||
{ | ||
glMatrixMode(GL_PROJECTION); | ||
glPushMatrix(); | ||
glLoadIdentity(); | ||
|
||
glOrtho(0, window->width, window->height, 0, -1000.0f, 1000.0f); | ||
|
||
glMatrixMode(GL_MODELVIEW); | ||
glLoadIdentity(); | ||
|
||
glEnable(GL_DEPTH_TEST); | ||
} | ||
|
||
void end_3d() | ||
{ | ||
glMatrixMode(GL_PROJECTION); | ||
glPopMatrix(); | ||
|
||
glMatrixMode(GL_MODELVIEW); | ||
glLoadIdentity(); | ||
|
||
glDisable(GL_DEPTH_TEST); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef _DRAWING_H_ | ||
#define _DRAWING_H_ | ||
|
||
#include <GLFW/glfw3.h> | ||
#include "window.h" | ||
|
||
typedef struct Color | ||
{ | ||
unsigned char red; | ||
unsigned char green; | ||
unsigned char blue; | ||
unsigned char alpha; | ||
} Color; | ||
|
||
#define GRAY (Color){ 130, 130, 130, 255 } | ||
#define BLACK (Color){ 0, 0, 0, 255 } | ||
#define RED (Color){ 255, 0, 0, 255 } | ||
#define GREEN (Color){ 0, 255, 0, 255 } | ||
#define BLUE (Color){ 0, 0, 255, 255 } | ||
|
||
void begin_drawing(struct Window * window); | ||
void end_drawing(struct Window * window); | ||
void clear_background(Color color); | ||
void draw_quad(Color color); | ||
void begin_3d(struct Window * window); | ||
void end_3d(); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "input.h" | ||
|
||
bool is_key_down(struct Window * window, int key) | ||
{ | ||
return glfwGetKey(window->handle, key); | ||
} | ||
|
||
bool is_key_up(struct Window * window, int key) | ||
{ | ||
return !glfwGetKey(window->handle, key); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#ifndef _INPUT_H_ | ||
#define _INPUT_H_ | ||
|
||
#include <GLFW/glfw3.h> | ||
#include <stdbool.h> | ||
#include "window.h" | ||
|
||
// Keyboard Function Keys | ||
#define KEY_SPACE 32 | ||
#define KEY_ESCAPE 256 | ||
#define KEY_ENTER 257 | ||
#define KEY_BACKSPACE 259 | ||
#define KEY_RIGHT 262 | ||
#define KEY_LEFT 263 | ||
#define KEY_DOWN 264 | ||
#define KEY_UP 265 | ||
#define KEY_F1 290 | ||
#define KEY_F2 291 | ||
#define KEY_F3 292 | ||
#define KEY_F4 293 | ||
#define KEY_F5 294 | ||
#define KEY_F6 295 | ||
#define KEY_F7 296 | ||
#define KEY_F8 297 | ||
#define KEY_F9 298 | ||
#define KEY_F10 299 | ||
#define KEY_F11 300 | ||
#define KEY_F12 301 | ||
#define KEY_LEFT_SHIFT 340 | ||
#define KEY_LEFT_CONTROL 341 | ||
#define KEY_LEFT_ALT 342 | ||
#define KEY_RIGHT_SHIFT 344 | ||
#define KEY_RIGHT_CONTROL 345 | ||
#define KEY_RIGHT_ALT 346 | ||
|
||
// Keyboard Alpha Numeric Keys | ||
#define KEY_ZERO 48 | ||
#define KEY_ONE 49 | ||
#define KEY_TWO 50 | ||
#define KEY_THREE 51 | ||
#define KEY_FOUR 52 | ||
#define KEY_FIVE 53 | ||
#define KEY_SIX 54 | ||
#define KEY_SEVEN 55 | ||
#define KEY_EIGHT 56 | ||
#define KEY_NINE 57 | ||
#define KEY_A 65 | ||
#define KEY_B 66 | ||
#define KEY_C 67 | ||
#define KEY_D 68 | ||
#define KEY_E 69 | ||
#define KEY_F 70 | ||
#define KEY_G 71 | ||
#define KEY_H 72 | ||
#define KEY_I 73 | ||
#define KEY_J 74 | ||
#define KEY_K 75 | ||
#define KEY_L 76 | ||
#define KEY_M 77 | ||
#define KEY_N 78 | ||
#define KEY_O 79 | ||
#define KEY_P 80 | ||
#define KEY_Q 81 | ||
#define KEY_R 82 | ||
#define KEY_S 83 | ||
#define KEY_T 84 | ||
#define KEY_U 85 | ||
#define KEY_V 86 | ||
#define KEY_W 87 | ||
#define KEY_X 88 | ||
#define KEY_Y 89 | ||
#define KEY_Z 90 | ||
|
||
bool is_key_down(struct Window * window, int key); | ||
bool is_key_up(struct Window * window, int key); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <stdlib.h> | ||
#include "window.h" | ||
|
||
struct Window * open_window(int width, int height, char * title) | ||
{ | ||
glfwInit(); | ||
|
||
struct Window * window = (struct Window *)malloc(sizeof(struct Window)); | ||
|
||
window->handle = glfwCreateWindow(width, height, title, NULL, NULL); | ||
window->width = width; | ||
window->height = height; | ||
|
||
glfwMakeContextCurrent(window->handle); | ||
|
||
int viewport_width, viewport_height; | ||
glfwGetFramebufferSize(window->handle, &viewport_width, &viewport_height); | ||
glViewport(0, 0, viewport_width, viewport_height); | ||
|
||
return window; | ||
} | ||
|
||
void close_window(struct Window * window) | ||
{ | ||
glfwDestroyWindow(window->handle); | ||
glfwTerminate(); | ||
free(window); | ||
} | ||
|
||
bool window_should_close(struct Window * window) | ||
{ | ||
return glfwWindowShouldClose(window->handle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef _WINDOW_H_ | ||
#define _WINDOW_H_ | ||
|
||
#include <GLFW/glfw3.h> | ||
#include <stdbool.h> | ||
|
||
struct Window | ||
{ | ||
GLFWwindow *handle; | ||
int width; | ||
int height; | ||
}; | ||
|
||
struct Window * open_window(int width, int height, char * title); | ||
void close_window(struct Window * window); | ||
bool window_should_close(struct Window * window); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "game.h" | ||
#include "core/window.h" | ||
#include "core/drawing.h" | ||
#include "core/input.h" | ||
|
||
struct game_state | ||
{ | ||
struct Window * window; | ||
}; | ||
|
||
static struct game_state *game_init() | ||
{ | ||
printf("Initializing Game\n"); | ||
struct game_state *state = malloc(sizeof(*state)); | ||
state->window = open_window(800, 450, "Neo UO"); | ||
return state; | ||
} | ||
|
||
static void game_finalize(struct game_state *state) | ||
{ | ||
close_window(state->window); | ||
free(state); | ||
} | ||
|
||
static void game_reload(struct game_state *state) | ||
{ | ||
|
||
} | ||
|
||
static void game_unload(struct game_state *state) | ||
{ | ||
} | ||
|
||
static bool game_step(struct game_state *state) | ||
{ | ||
if(window_should_close(state->window) || | ||
is_key_down(state->window, KEY_ESCAPE)) | ||
{ | ||
return false; | ||
} | ||
|
||
begin_drawing(state->window); | ||
|
||
clear_background(BLACK); | ||
|
||
//begin_3d(state->window); | ||
draw_quad(RED); | ||
//end_3d(); | ||
|
||
end_drawing(state->window); | ||
|
||
glfwPollEvents(); | ||
|
||
return true; | ||
} | ||
|
||
const struct game_api GAME_API = | ||
{ | ||
.init = game_init, | ||
.reload = game_reload, | ||
.step = game_step, | ||
.unload = game_unload, | ||
.finalize = game_finalize | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef _GAME_H_ | ||
#define _GAME_H_ | ||
|
||
#include <stdbool.h> | ||
|
||
struct game_state; | ||
|
||
struct game_api | ||
{ | ||
// Returns a fresh game state | ||
struct game_state *(*init)(); | ||
|
||
// Destroys a game state | ||
void (*finalize)(struct game_state *state); | ||
|
||
// Called exactly once when the game code is reloaded | ||
void (*reload)(struct game_state *state); | ||
|
||
// Called exactly once when the game code is about to be reloaded | ||
void (*unload)(struct game_state *state); | ||
|
||
// Called at a regular interval by the main program | ||
// Returns true if the program should continue running | ||
bool (*step)(struct game_state *state); | ||
}; | ||
|
||
extern const struct game_api GAME_API; | ||
|
||
#endif |
Oops, something went wrong.