blob: 7fe5d693f137f8ab56f6a0c50b3412f168e1289d [file] [log] [blame]
swissChilidbbd5402020-08-07 15:07:39 -07001#include "gui.h"
2
3#include <GL/glew.h>
4#include <SDL2/SDL.h>
5#include <SDL2/SDL_opengl.h>
6
7#define NK_INCLUDE_FIXED_TYPES
8#define NK_INCLUDE_STANDARD_IO
9#define NK_INCLUDE_STANDARD_VARARGS
10#define NK_INCLUDE_DEFAULT_ALLOCATOR
11#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
12#define NK_INCLUDE_FONT_BAKING
13#define NK_INCLUDE_DEFAULT_FONT
14#define NK_IMPLEMENTATION
15#define NK_SDL_GL3_IMPLEMENTATION
16#include "nuklear/nuklear.h"
17#include "nuklear/demo/sdl_opengl3/nuklear_sdl_gl3.h"
18
19#define WINDOW_WIDTH 720
20#define WINDOW_HEIGHT 640
21#define MAX_VERTEX_MEMORY 512 * 1024
22#define MAX_ELEMENT_MEMORY 128 * 1024
23
24void gui(cpu_t *cpu)
25{
26 SDL_Window *win;
27 SDL_GLContext glContext;
28 int win_width, win_height;
29 bool running = true;
30 bool cpu_running = false;
31
32 struct nk_context *ctx;
33 struct nk_colorf bg;
34
35 SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
36 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS);
37 SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
38 SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
39 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
40 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
41 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
42 win = SDL_CreateWindow("6502",
43 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
44 WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
45 glContext = SDL_GL_CreateContext(win);
46 SDL_GetWindowSize(win, &win_width, &win_height);
47
48 /* OpenGL setup */
49 glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
50 glewExperimental = 1;
51 if (glewInit() != GLEW_OK) {
52 fprintf(stderr, "Failed to setup GLEW\n");
53 exit(1);
54 }
55
56 ctx = nk_sdl_init(win);
57
58 struct nk_font_atlas *atlas;
59 nk_sdl_font_stash_begin(&atlas);
60 struct nk_font *font = nk_font_atlas_add_default(atlas, 16, NULL);
61 nk_sdl_font_stash_end();
62 //nk_style_load_all_cursors(ctx, atlas->cursors);
63 nk_style_set_font(ctx, &font->handle);
64
65 bg.r = 0.29f, bg.g = 0.28f, bg.b = 0.50f, bg.a = 1.0f;
66
67 while (running)
68 {
69 SDL_Event evt;
70 nk_input_begin(ctx);
71 while (SDL_PollEvent(&evt))
72 {
73 if (evt.type == SDL_QUIT) goto cleanup;
74 nk_sdl_handle_event(&evt);
75 }
76 nk_input_end(ctx);
77
78 if (cpu_running && cpu->running)
79 step(cpu);
80
81 if (!cpu->running)
82 cpu_running = false;
83
84 if (nk_begin(ctx, "Registers", nk_rect(50, 300, 400, 90),
85 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
86 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
87 {
88 nk_layout_row_dynamic(ctx, 30, 4);
89 char regpc[12],
90 rega[12],
91 regx[12],
92 regy[12];
93 sprintf(regpc, "PC: $%x", cpu->pc);
94 sprintf(rega, "A: $%x", cpu->regs[A]);
95 sprintf(regx, "X: $%x", cpu->regs[X]);
96 sprintf(regy, "Y: $%x", cpu->regs[Y]);
97 cpu->pc = nk_propertyi(ctx, "PC", 0, cpu->pc, 0xFFFF, 1, 20.0f);
98 cpu->regs[A] = nk_propertyi(ctx, "A", 0, cpu->regs[A], 0xFF, 1, 20.0f);
99 cpu->regs[X] = nk_propertyi(ctx, "X", 0, cpu->regs[X], 0xFF, 1, 20.0f);
100 cpu->regs[Y] = nk_propertyi(ctx, "Y", 0, cpu->regs[Y], 0xFF, 1, 20.0f);
101 }
102 nk_end(ctx);
103
104 if (nk_begin(ctx, "Debugger", nk_rect(50, 50, 230, 150),
105 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
106 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
107 {
108 nk_layout_row_dynamic(ctx, 30, 2);
109 nk_label(ctx, cpu->running ? "CPU Running" : "CPU Halted", NK_TEXT_LEFT);
110 if (nk_button_label(ctx, "Reset"))
111 {
112 puts("cpu reset");
113 reset(cpu);
114 }
115
116 nk_layout_row_dynamic(ctx, 30, 2);
117 if (nk_button_label(ctx, "Step"))
118 {
119 printf("step pressed!\n");
120 step(cpu);
121 }
122
123 if (nk_button_label(ctx, cpu_running ? "Stop" : "Run"))
124 {
125 cpu_running = !cpu_running;
126 puts(cpu_running ? "cpu running" : "cpu stopped");
127 }
128 }
129 nk_end(ctx);
130
131 SDL_GetWindowSize(win, &win_width, &win_height);
132 glViewport(0, 0, win_width, win_height);
133 glClear(GL_COLOR_BUFFER_BIT);
134 glClearColor(bg.r, bg.g, bg.b, bg.a);
135 nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
136 SDL_GL_SwapWindow(win);
137 }
138
139cleanup:
140 nk_sdl_shutdown();
141 SDL_GL_DeleteContext(glContext);
142 SDL_DestroyWindow(win);
143 SDL_Quit();
144}