1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-06-11 05:29:33 +00:00

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.
This commit is contained in:
Peter Evans 2017-12-16 22:45:39 -06:00
parent 294fab76aa
commit b646bfc511
6 changed files with 71 additions and 0 deletions

View File

@ -17,3 +17,6 @@ link_directories(/usr/local/lib)
# our bullshit
add_executable(erc ${sources} src/main.c)
# Graphics
target_link_libraries(erc glfw)

View File

@ -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 *, ...);

View File

@ -1,6 +1,9 @@
#ifndef _VM_SCREEN_H_
#define _VM_SCREEN_H_
#include <GLFW/glfw3.h>
#include <stdbool.h>
/*
* 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();

View File

@ -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");
}

View File

@ -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.
*/

View File

@ -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)