From b646bfc511cef8c55d2edb074a1a12adfbe70fd0 Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Sat, 16 Dec 2017 22:45:39 -0600 Subject: [PATCH] First stab at adding graphics. This involves using glfw. This first commit creates a window but doesn't do anything with it; it also just hangs until you can escape out somehow. --- CMakeLists.txt | 3 +++ include/log.h | 1 + include/vm_screen.h | 8 ++++++++ src/main.c | 20 ++++++++++++++++++++ src/vm_screen.c | 36 ++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 3 +++ 6 files changed, 71 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 314e427..99cdbec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,3 +17,6 @@ link_directories(/usr/local/lib) # our bullshit add_executable(erc ${sources} src/main.c) + +# Graphics +target_link_libraries(erc glfw) diff --git a/include/log.h b/include/log.h index 6d1d22c..b305068 100644 --- a/include/log.h +++ b/include/log.h @@ -12,6 +12,7 @@ enum log_errcode { ERR_OOM, // out of memory ERR_OOB, // out of bounds ERR_BADFILE, + ERR_GFXINIT, // couldn't initialize graphics }; extern void log_write(int, const char *, ...); diff --git a/include/vm_screen.h b/include/vm_screen.h index c154484..a28fc49 100644 --- a/include/vm_screen.h +++ b/include/vm_screen.h @@ -1,6 +1,9 @@ #ifndef _VM_SCREEN_H_ #define _VM_SCREEN_H_ +#include +#include + /* * If you just want to plot a single pixel, you can use this macro to * abstract away the need to indicate the x/y dimensions (as those must @@ -10,6 +13,8 @@ vm_screen_draw_rect(context, xpos, ypos, 1, 1) typedef struct { + GLFWwindow *window; + /* * These form the components of an RGBA composite color. */ @@ -19,6 +24,9 @@ typedef struct { int color_alpha; } vm_screen_context; +extern int vm_screen_add_window(vm_screen_context *); +extern int vm_screen_init(); +extern void vm_screen_finish(); extern void vm_screen_draw_rect(vm_screen_context *, int, int, int, int); extern void vm_screen_free_context(vm_screen_context *); extern vm_screen_context *vm_screen_new_context(); diff --git a/src/main.c b/src/main.c index eea527f..31d3e7b 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include "apple2.h" #include "log.h" #include "option.h" +#include "vm_screen.h" /* * This function will establish the base environment that we want to use @@ -43,6 +44,8 @@ init(int argc, char **argv) // We're literally using stdout in this heavy phase of development. log_open(stdout); + + vm_screen_init(); } /* @@ -61,6 +64,8 @@ finish() } log_close(); + + vm_screen_finish(); } /* @@ -71,6 +76,7 @@ int main(int argc, char **argv) { apple2 *mach; + vm_screen_context *context; int err; init(argc, argv); @@ -88,6 +94,20 @@ main(int argc, char **argv) exit(1); } + context = vm_screen_new_context(); + if (context == NULL) { + fprintf(stderr, "Screen context failed!\n"); + exit(1); + } + + if (!vm_screen_add_window(context)) { + fprintf(stderr, "Window creation failed!\n"); + exit(1); + } + + for (;;) { + } + // ha ha ha ha #nervous #laughter printf("Hello, world\n"); } diff --git a/src/vm_screen.c b/src/vm_screen.c index 65fe7dd..0895a69 100644 --- a/src/vm_screen.c +++ b/src/vm_screen.c @@ -11,6 +11,22 @@ #include "log.h" #include "vm_screen.h" +int +vm_screen_init() +{ + if (!glfwInit()) { + return ERR_GFXINIT; + } + + return OK; +} + +void +vm_screen_finish() +{ + glfwTerminate(); +} + /* * Return a new screen context. We also set the color to black. */ @@ -29,6 +45,20 @@ vm_screen_new_context() return context; } +int +vm_screen_add_window(vm_screen_context *context) +{ + context->window = glfwCreateWindow(320, 240, "erc", NULL, NULL); + if (context->window == NULL) { + log_critical("Could not create a window"); + return ERR_GFXINIT; + } + + glfwMakeContextCurrent(context->window); + + return OK; +} + /* * Free the contents of a screen context. */ @@ -38,6 +68,12 @@ vm_screen_free_context(vm_screen_context *context) free(context); } +bool +vm_screen_active(vm_screen_context *context) +{ + return !glfwWindowShouldClose(context->window); +} + /* * Set the color of a screen context to a given RGBA value. */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3db6788..0015186 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,3 +23,6 @@ add_executable(erc-test ${sources} ${test_sources}) # Our unit-testing library target_link_libraries(erc-test criterion) + +# Graphics +target_link_libraries(erc-test glfw)