diff options
| -rwxr-xr-x | build.sh | 3 | ||||
| -rw-r--r-- | dc/dc.c | 12 | ||||
| -rw-r--r-- | platform/gl/gl.c | 16 | ||||
| -rw-r--r-- | platform/x11/x11.c | 6 | ||||
| -rw-r--r-- | teensy/common.h | 14 | ||||
| -rw-r--r-- | teensy/context.h | 10 | ||||
| -rw-r--r-- | teensy/renderer.h | 10 | ||||
| -rw-r--r-- | teensy/teensy.h | 18 | ||||
| -rw-r--r-- | teensy/teensy_common.c (renamed from teensy/common.c) | 2 | ||||
| -rw-r--r-- | teensy/teensy_common.h | 15 | ||||
| -rw-r--r-- | teensy/teensy_context.c (renamed from teensy/context.c) | 35 | ||||
| -rw-r--r-- | teensy/teensy_context.h | 12 | ||||
| -rw-r--r-- | teensy/teensy_list.c (renamed from teensy/dyn_arr.c) | 2 | ||||
| -rw-r--r-- | teensy/teensy_list.h (renamed from teensy/dyn_arr.h) | 16 | ||||
| -rw-r--r-- | teensy/teensy_log.c (renamed from teensy/log.c) | 4 | ||||
| -rw-r--r-- | teensy/teensy_log.h (renamed from teensy/log.h) | 6 | ||||
| -rw-r--r-- | teensy/teensy_mem.c (renamed from teensy/mem.c) | 6 | ||||
| -rw-r--r-- | teensy/teensy_mem.h (renamed from teensy/mem.h) | 6 | ||||
| -rw-r--r-- | teensy/teensy_platform.h (renamed from teensy/platform.h) | 6 | ||||
| -rw-r--r-- | teensy/teensy_renderer.c (renamed from teensy/renderer.c) | 30 | ||||
| -rw-r--r-- | teensy/teensy_renderer.h | 10 |
21 files changed, 128 insertions, 111 deletions
@@ -1,6 +1,6 @@ #!/bin/bash -cc=gcc +cc=cc cflags=(-Wall -Wextra -std=gnu99 -O2) ldflags=(-lm) @@ -26,6 +26,7 @@ if [ ! -z "$clean" ]; then exit fi +[ ! -z "$debug" ] && cc=tcc # TCC is faster for debug [ ! -z "$debug" ] && cflags+=(-g -ggdb -rdynamic) teensy_cflags=(${cflags[@]} -fPIC -Iteensy -Iplatform/$platform) @@ -14,10 +14,10 @@ int main(void) { - struct ty_creation_hints hints = { - .win_width = SCREEN_WIDTH, - .win_height = SCREEN_HEIGHT, - .win_title = "Demonchime", + struct ty_hints hints = { + .scr_width = SCREEN_WIDTH, + .scr_height = SCREEN_HEIGHT, + .game_title = "Demonchime", .ticrate = 30, }; ty_init(hints); @@ -30,6 +30,8 @@ int main(void) free(data); while (ty_is_game_running()) { + clock_t start = clock(); + ty_draw_clear(TY_COLOR_BLACK); for (int y = 0; y < SCREEN_HEIGHT; y += 16) { @@ -75,6 +77,8 @@ int main(void) ty_tick(); ty_sleep(1000 / hints.ticrate); + + ty_log_info("%g ms", (float)(clock() - start) / CLOCKS_PER_SEC); } ty_free_image(img); diff --git a/platform/gl/gl.c b/platform/gl/gl.c index 4072949..b6c3d2a 100644 --- a/platform/gl/gl.c +++ b/platform/gl/gl.c @@ -5,9 +5,9 @@ #include <glad/glad.h> #include <GLFW/glfw3.h> -#include "common.h" #include "teensy.h" -#include "platform.h" +#include "teensy_common.h" +#include "teensy_platform.h" static char vert[] = "#version 330 core\n" @@ -75,9 +75,9 @@ void ty_platform_init(struct ty_ctx *ctx) glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); p.win = glfwCreateWindow( - ctx->creation_hints.win_width, - ctx->creation_hints.win_height, - ctx->creation_hints.win_title, + ctx->hints.scr_width * 3, + ctx->hints.scr_height * 3, + ctx->hints.game_title, NULL, NULL ); @@ -102,6 +102,8 @@ void ty_platform_init(struct ty_ctx *ctx) glGenTextures(1, &p.screen_handle); glBindTexture(GL_TEXTURE_2D, p.screen_handle); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glGenVertexArrays(1, &p.vao); glGenBuffers(1, &p.vbo); @@ -149,6 +151,10 @@ void ty_platform_init(struct ty_ctx *ctx) void ty_platform_deinit(void) { + glDeleteProgram(p.program); + glDeleteBuffers(1, &p.vbo); + glDeleteVertexArrays(1, &p.vao); + glDeleteTextures(1, &p.screen_handle); glfwDestroyWindow(p.win); glfwTerminate(); } diff --git a/platform/x11/x11.c b/platform/x11/x11.c index c117c47..2dde168 100644 --- a/platform/x11/x11.c +++ b/platform/x11/x11.c @@ -11,9 +11,9 @@ #include <X11/Xlib.h> #include <X11/Xutil.h> -#include "common.h" #include "teensy.h" -#include "platform.h" +#include "teensy_common.h" +#include "teensy_platform.h" #define SEC2NANO ((uint64_t)1000000000) @@ -70,7 +70,7 @@ void ty_platform_init(struct ty_ctx *ctx) p.dis, p.root, 0, 0, - ctx->creation_hints.win_width, ctx->creation_hints.win_height, + ctx->hints.scr_width, ctx->hints.scr_height, 0, DefaultDepth(p.dis, p.scr), CopyFromParent, diff --git a/teensy/common.h b/teensy/common.h deleted file mode 100644 index 6e3e66f..0000000 --- a/teensy/common.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef COMMON_H_ -#define COMMON_H_ - -#include <stdbool.h> -#include <stdint.h> -#include <stdlib.h> - -#include "log.h" -#include "mem.h" - -#define TY_PI 3.14 -#define TY_DEG2RAD (180 / PI) - -#endif // COMMON_H_ diff --git a/teensy/context.h b/teensy/context.h deleted file mode 100644 index 0bdc5f9..0000000 --- a/teensy/context.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef CONTEXT_H_ -#define CONTEXT_H_ - -#include "common.h" -#include "renderer.h" -#include "teensy.h" - -extern struct ty_ctx ctx; - -#endif // CONTEXT_H_ diff --git a/teensy/renderer.h b/teensy/renderer.h deleted file mode 100644 index 70eac72..0000000 --- a/teensy/renderer.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef RENDERER_H_ -#define RENDERER_H_ - -#include "common.h" -#include "teensy.h" - -void ty_init_renderer(void); -void ty_deinit_renderer(void); - -#endif // RENDERER_H_ diff --git a/teensy/teensy.h b/teensy/teensy.h index c94986f..1f082f9 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -1,9 +1,9 @@ #ifndef TEENSY_H_ #define TEENSY_H_ -#include "common.h" -#include "context.h" -#include "dyn_arr.h" +#include "teensy_common.h" +#include "teensy_context.h" +#include "teensy_list.h" #define ty_vec2(x, y) ((struct ty_vec2){x, y}) #define ty_vec2i(x, y) ((struct ty_vec2i){x, y}) @@ -19,15 +19,15 @@ #define TY_COLOR_CYAN ty_color(0, 255, 255) #define TY_COLOR_WHITE ty_color(255, 255, 255) -struct ty_creation_hints { - int win_width; - int win_height; - const char *win_title; +struct ty_hints { + int scr_width; + int scr_height; + const char *game_title; int ticrate; }; struct ty_ctx { - struct ty_creation_hints creation_hints; + struct ty_hints hints; double ticrate; double prev_tic_ts; }; @@ -72,7 +72,7 @@ struct ty_renderer { struct ty_image screen; }; -void ty_init(struct ty_creation_hints creation_hints); +void ty_init(struct ty_hints hints); void ty_deinit(void); // Whether or not the main loop should continue executing. diff --git a/teensy/common.c b/teensy/teensy_common.c index 4da4004..96d589e 100644 --- a/teensy/common.c +++ b/teensy/teensy_common.c @@ -1,4 +1,4 @@ -#include "common.h" +#include "teensy_common.h" #include <stdarg.h> #include <stdio.h> diff --git a/teensy/teensy_common.h b/teensy/teensy_common.h new file mode 100644 index 0000000..dc631cb --- /dev/null +++ b/teensy/teensy_common.h @@ -0,0 +1,15 @@ +#ifndef TEENSY_COMMON_H_ +#define TEENSY_COMMON_H_ + +#include <assert.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdlib.h> + +#include "teensy_log.h" +#include "teensy_mem.h" + +#define TY_PI 3.14 +#define TY_DEG2RAD (180 / PI) + +#endif // TEENSY_COMMON_H_ diff --git a/teensy/context.c b/teensy/teensy_context.c index 060254c..4a812dd 100644 --- a/teensy/context.c +++ b/teensy/teensy_context.c @@ -1,21 +1,27 @@ -#include "context.h" -#include "renderer.h" +#include "teensy_context.h" +#include "teensy_renderer.h" #include <math.h> #include <time.h> -#include "platform.h" +#include "teensy_platform.h" struct ty_ctx ctx; extern struct ty_renderer r; -void ty_init(struct ty_creation_hints creation_hints) +bool is_init(void) +{ + int scr_size = ctx.hints.scr_width * ctx.hints.scr_height; + return scr_size > 0; +} + +void ty_init(struct ty_hints hints) { ty_init_mem(); - ctx.creation_hints = creation_hints; - ctx.ticrate = 1.0 / creation_hints.ticrate; - ctx.prev_tic_ts = 0; + ctx.hints = hints; + ctx.ticrate = 1.0 / hints.ticrate; + ctx.prev_tic_ts = 0; ty_platform_init(&ctx); ty_init_renderer(); @@ -23,6 +29,7 @@ void ty_init(struct ty_creation_hints creation_hints) void ty_deinit(void) { + assert(is_init()); ty_platform_deinit(); ty_deinit_renderer(); ty_deinit_mem(); @@ -30,16 +37,20 @@ void ty_deinit(void) bool ty_is_game_running(void) { + assert(is_init()); return !ty_platform_os_wants_quit(); } double ty_get_time(void) { + assert(is_init()); return ty_platform_get_time(); } int ty_tick(void) { + assert(is_init()); + double current_time = ty_get_time(); double time_since_tic = current_time - ctx.prev_tic_ts; @@ -54,8 +65,10 @@ int ty_tick(void) // TODO: Find a better place for this void ty_sleep(uint64_t ms) { - struct timespec ts; - ts.tv_sec = ms / 1000; - ts.tv_nsec = (ms % 1000) * 1000000; - nanosleep(&ts, NULL); + assert(is_init()); + + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000; + nanosleep(&ts, NULL); } diff --git a/teensy/teensy_context.h b/teensy/teensy_context.h new file mode 100644 index 0000000..056a0c2 --- /dev/null +++ b/teensy/teensy_context.h @@ -0,0 +1,12 @@ +#ifndef TEENSY_CONTEXT_H_ +#define TEENSY_CONTEXT_H_ + +#include "teensy_common.h" +#include "teensy_renderer.h" +#include "teensy.h" + +extern struct ty_ctx ctx; + +bool is_init(void); + +#endif // TEENSY_CONTEXT_H_ diff --git a/teensy/dyn_arr.c b/teensy/teensy_list.c index 46e619d..c8035b8 100644 --- a/teensy/dyn_arr.c +++ b/teensy/teensy_list.c @@ -1,4 +1,4 @@ -#include "dyn_arr.h" +#include "teensy_list.h" #include <stdio.h> #include <assert.h> diff --git a/teensy/dyn_arr.h b/teensy/teensy_list.h index a764cda..dd1e06a 100644 --- a/teensy/dyn_arr.h +++ b/teensy/teensy_list.h @@ -1,8 +1,8 @@ -#ifndef DYN_ARR_H_ -#define DYN_ARR_H_ +#ifndef TEENSY_LIST_H_ +#define TEENSY_LIST_H_ -#include "common.h" -#include "mem.h" +#include "teensy_common.h" +#include "teensy_mem.h" struct ty_list_header { size_t cap; @@ -23,11 +23,11 @@ struct ty_list_header { if (amt > header->cap) { \ header->cap = header->cap < TY_LIST_MIN_CAP \ ? TY_LIST_MIN_CAP \ - : header->cap * TY_LIST_GROW_RATE \ + : header->cap * TY_LIST_GROW_RATE; \ header = ty_realloc( \ header, \ - (sizeof(*(arr)) * count) + sizeof(struct ty_list_header) \ - ); \ + (sizeof(*(arr)) * amt) + sizeof(struct ty_list_header) \ + ); \ (arr) = (void*)(header + 1); \ } \ } while (0) @@ -46,4 +46,4 @@ struct ty_list_header { void *ty_list_create(void); -#endif // DYN_ARR_H_ +#endif // TEENSY_LIST_H_ diff --git a/teensy/log.c b/teensy/teensy_log.c index fbd96a2..26672a4 100644 --- a/teensy/log.c +++ b/teensy/teensy_log.c @@ -1,8 +1,8 @@ -#include "log.h" +#include "teensy_log.h" #include <stdarg.h> -#include "common.h" +#include "teensy_common.h" void ty_log_msg( FILE *file, diff --git a/teensy/log.h b/teensy/teensy_log.h index cebf39b..825ce55 100644 --- a/teensy/log.h +++ b/teensy/teensy_log.h @@ -1,5 +1,5 @@ -#ifndef LOG_H_ -#define LOG_H_ +#ifndef TEENSY_LOG_H_ +#define TEENSY_LOG_H_ #include <stdio.h> #include <stdlib.h> @@ -44,4 +44,4 @@ void ty_log_msg( void _ty_log_plain(FILE *file, const char *fmt, ...); -#endif // LOG_H_ +#endif // TEENSY_LOG_H_ diff --git a/teensy/mem.c b/teensy/teensy_mem.c index fa885ca..2a6d992 100644 --- a/teensy/mem.c +++ b/teensy/teensy_mem.c @@ -1,7 +1,7 @@ -#include "mem.h" +#include "teensy_mem.h" -#include "common.h" -#include "dyn_arr.h" +#include "teensy_common.h" +#include "teensy_list.h" // Array to track temp allocations. void **temp_allocations; diff --git a/teensy/mem.h b/teensy/teensy_mem.h index 61f3ee2..da2fed9 100644 --- a/teensy/mem.h +++ b/teensy/teensy_mem.h @@ -1,5 +1,5 @@ -#ifndef MEM_H_ -#define MEM_H_ +#ifndef TEENSY_MEM_H_ +#define TEENSY_MEM_H_ #include <stdlib.h> @@ -21,4 +21,4 @@ void ty_free(void *ptr); // Returns -1 in release builds. int ty_alloc_count(void); -#endif // MEM_H_ +#endif // TEENSY_MEM_H_ diff --git a/teensy/platform.h b/teensy/teensy_platform.h index df3c5f8..7733be1 100644 --- a/teensy/platform.h +++ b/teensy/teensy_platform.h @@ -1,5 +1,5 @@ -#ifndef PLATFORM_H_ -#define PLATFORM_H_ +#ifndef TEENSY_PLATFORM_H_ +#define TEENSY_PLATFORM_H_ // This platform functions are to be implemented by a platform library. @@ -9,4 +9,4 @@ void ty_platform_frame(struct ty_image img); bool ty_platform_os_wants_quit(void); double ty_platform_get_time(void); -#endif // PLATFORM_H_ +#endif // TEENSY_PLATFORM_H_ diff --git a/teensy/renderer.c b/teensy/teensy_renderer.c index 29dc871..b29e485 100644 --- a/teensy/renderer.c +++ b/teensy/teensy_renderer.c @@ -1,28 +1,18 @@ -#include "renderer.h" +#include "teensy_renderer.h" #include <stdio.h> #include <string.h> #include <math.h> -#include "context.h" -#include "dyn_arr.h" +#include "teensy_context.h" +#include "teensy_list.h" struct ty_renderer r; static -void renderer_init_check() +bool is_renderer_init(void) { -#ifdef TEENSY_DEBUG - if ( - r.screen.data == NULL || - r.screen.width * r.screen.height == 0 - ) { - ty_log_fatal( - TY_ERR_RENDERER_NOT_INIT, - "renderer is not yet intialized" - ); - } -#endif + return r.screen.data != NULL && r.screen.width * r.screen.height != 0; } static @@ -51,15 +41,15 @@ void image_bounds_check(struct ty_image img, struct ty_vec2i pos) void ty_init_renderer(void) { r.screen = ty_create_image( - ctx.creation_hints.win_width, - ctx.creation_hints.win_height, + ctx.hints.scr_width, + ctx.hints.scr_height, NULL ); } void ty_deinit_renderer(void) { - renderer_init_check(); + assert(is_renderer_init()); ty_free_image(r.screen); } @@ -103,7 +93,7 @@ void ty_img_set_pixel( void ty_draw_clear(struct ty_color col) { - renderer_init_check(); + assert(is_renderer_init()); for (int i = 0; i < r.screen.width * r.screen.height; i++) { r.screen.data[i] = col; } @@ -223,5 +213,5 @@ void ty_draw_line( void ty_draw_end(void) { - renderer_init_check(); + assert(is_renderer_init()); } diff --git a/teensy/teensy_renderer.h b/teensy/teensy_renderer.h new file mode 100644 index 0000000..7002890 --- /dev/null +++ b/teensy/teensy_renderer.h @@ -0,0 +1,10 @@ +#ifndef TEENSY_RENDERER_H_ +#define TEENSY_RENDERER_H_ + +#include "teensy_common.h" +#include "teensy.h" + +void ty_init_renderer(void); +void ty_deinit_renderer(void); + +#endif // TEENSY_RENDERER_H_ |
