blob: e2bf5e0758c40b71fc0e1c77b3bda5c2c6fed216 [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
swissChilicc27cfe2020-08-08 12:57:57 -07006#ifndef NO_PTHREAD
7#include <pthread.h>
8#endif
9
swissChilibb478f12020-08-07 20:45:07 -070010struct nk_color byte_to_color(uint8_t b)
11{
12 struct nk_color c;
swissChili94ba1f52020-08-08 11:39:10 -070013 c.r = (b >> 5) * (255 / 0b111);
swissChilibb478f12020-08-07 20:45:07 -070014 c.g = ((b >> 2) & 0b111) * (255 / 0b111);
15 c.b = (b & 0b11) * (255 / 0b11);
16 c.a = 255;
17 return c;
18}
19
20void screen(struct nk_context *ctx, uint8_t *mem, uint8_t size)
21{
22 struct nk_command_buffer *out = nk_window_get_canvas(ctx);
23
24 struct nk_rect bounds;
25 enum nk_widget_layout_states state = nk_widget(&bounds, ctx);
26
27 if (!state)
28 return;
29
30 //nk_fill_rect(out, bounds, 0, nk_rgb(255, 0, 0));
31
32 //return;
33
34 for (int i = 0; i < CPU_FB_H; i++)
35 {
36 for (int j = 0; j < CPU_FB_W; j++)
37 {
38 nk_fill_rect(out,
39 nk_rect(bounds.x + i * size, bounds.y + j * size,
40 size, size), 0.0f,
swissChili94ba1f52020-08-08 11:39:10 -070041 byte_to_color(mem[i + CPU_FB_H * j]));
swissChilibb478f12020-08-07 20:45:07 -070042 }
43 }
44}
swissChili94ba1f52020-08-08 11:39:10 -070045
46sdl_screen_t new_sdl_screen(uint8_t size)
47{
48 sdl_screen_t scr;
49 scr.win = SDL_CreateWindow("6502",
50 SDL_WINDOWPOS_CENTERED,
51 SDL_WINDOWPOS_CENTERED,
52 size * 32,
53 size * 32,
54 0);
55 scr.size = size;
56 scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
swissChilicc27cfe2020-08-08 12:57:57 -070057 scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
58 CPU_FB_W, CPU_FB_H);
swissChili94ba1f52020-08-08 11:39:10 -070059
60 return scr;
61}
62
swissChilicc27cfe2020-08-08 12:57:57 -070063void free_sdl_screen(sdl_screen_t *scr)
swissChili94ba1f52020-08-08 11:39:10 -070064{
swissChilicc27cfe2020-08-08 12:57:57 -070065 //free(scr->fb);
66 SDL_DestroyTexture(scr->tex);
67 SDL_DestroyRenderer(scr->r);
68 SDL_DestroyWindow(scr->win);
69}
70
71bool sdl_screen(sdl_screen_t *scr, uint8_t *mem, bool dirty)
72{
73 static bool texture_set = false;
74 if (!texture_set)
75 {
76 SDL_UpdateTexture(scr->tex, NULL, mem, CPU_FB_W);
77 }
swissChili94ba1f52020-08-08 11:39:10 -070078
79 SDL_Event e;
80
81 while (SDL_PollEvent(&e))
82 {
83 switch (e.type)
84 {
85 case SDL_QUIT:
swissChilicc27cfe2020-08-08 12:57:57 -070086 return true;
swissChili94ba1f52020-08-08 11:39:10 -070087 }
88 }
89
swissChilicc27cfe2020-08-08 12:57:57 -070090 if (dirty)
swissChili94ba1f52020-08-08 11:39:10 -070091 {
swissChilicc27cfe2020-08-08 12:57:57 -070092 SDL_RenderClear(scr->r);
93 SDL_RenderCopy(scr->r, scr->tex, NULL, NULL);
94 SDL_RenderPresent(scr->r);
swissChili94ba1f52020-08-08 11:39:10 -070095 }
96
swissChilicc27cfe2020-08-08 12:57:57 -070097 return false;
swissChili94ba1f52020-08-08 11:39:10 -070098}
swissChilicc27cfe2020-08-08 12:57:57 -070099
100
101#ifndef NO_PTHREAD
102
103void *screen_thread(uint8_t *mem)
104{
105 sdl_screen_t scr = new_sdl_screen(8);
106 while (true)
107 {
108 if (sdl_screen(&scr, mem, true))
109 break;
110 }
111 free_sdl_screen(&scr);
112
113 exit(0);
114
115 return NULL;
116}
117
118void start_screen_thread(uint8_t *mem)
119{
120 pthread_t thread;
121 pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem);
122}
123
124#endif