blob: fedc15108380e564e8680b118484bdb5445b5d6b [file] [log] [blame]
swissChilibb478f12020-08-07 20:45:07 -07001#include "screen.h"
2#include "cpu.h"
3
swissChili94ba1f52020-08-08 11:39:10 -07004#include <SDL2/SDL.h>
5
swissChilibb478f12020-08-07 20:45:07 -07006struct nk_color byte_to_color(uint8_t b)
7{
8 struct nk_color c;
swissChili94ba1f52020-08-08 11:39:10 -07009 c.r = (b >> 5) * (255 / 0b111);
swissChilibb478f12020-08-07 20:45:07 -070010 c.g = ((b >> 2) & 0b111) * (255 / 0b111);
11 c.b = (b & 0b11) * (255 / 0b11);
12 c.a = 255;
13 return c;
14}
15
16void screen(struct nk_context *ctx, uint8_t *mem, uint8_t size)
17{
18 struct nk_command_buffer *out = nk_window_get_canvas(ctx);
19
20 struct nk_rect bounds;
21 enum nk_widget_layout_states state = nk_widget(&bounds, ctx);
22
23 if (!state)
24 return;
25
26 //nk_fill_rect(out, bounds, 0, nk_rgb(255, 0, 0));
27
28 //return;
29
30 for (int i = 0; i < CPU_FB_H; i++)
31 {
32 for (int j = 0; j < CPU_FB_W; j++)
33 {
34 nk_fill_rect(out,
35 nk_rect(bounds.x + i * size, bounds.y + j * size,
36 size, size), 0.0f,
swissChili94ba1f52020-08-08 11:39:10 -070037 byte_to_color(mem[i + CPU_FB_H * j]));
swissChilibb478f12020-08-07 20:45:07 -070038 }
39 }
40}
swissChili94ba1f52020-08-08 11:39:10 -070041
42sdl_screen_t new_sdl_screen(uint8_t size)
43{
44 sdl_screen_t scr;
45 scr.win = SDL_CreateWindow("6502",
46 SDL_WINDOWPOS_CENTERED,
47 SDL_WINDOWPOS_CENTERED,
48 size * 32,
49 size * 32,
50 0);
51 scr.size = size;
52 scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
53
54 return scr;
55}
56
57void sdl_screen(sdl_screen_t *scr, uint8_t *mem)
58{
59 SDL_RenderClear(scr->r);
60
61 SDL_Event e;
62
63 while (SDL_PollEvent(&e))
64 {
65 switch (e.type)
66 {
67 case SDL_QUIT:
68 exit(0);
69 }
70 }
71
72 for (int i = 0; i < CPU_FB_H; i++)
73 {
74 for (int j = 0; j < CPU_FB_W; j++)
75 {
76 SDL_Rect r =
77 {
78 i * scr->size,
79 j * scr->size,
80 scr->size,
81 scr->size,
82 };
83
84 struct nk_color c = byte_to_color(mem[i + CPU_FB_H * j]);
85
86 SDL_SetRenderDrawColor(scr->r, c.r, c.g, c.b, c.a);
87 SDL_RenderFillRect(scr->r, &r);
88 }
89 }
90
91 SDL_RenderPresent(scr->r);
92}