blob: e19baa23bf59caf1ca71582b5d5ae1430472d61d [file] [log] [blame]
swissChilibb478f12020-08-07 20:45:07 -07001#include "screen.h"
2#include "cpu.h"
swissChilib71e0272020-08-08 15:56:14 -07003#include "common.h"
swissChilibb478f12020-08-07 20:45:07 -07004
swissChili94ba1f52020-08-08 11:39:10 -07005#include <SDL2/SDL.h>
6
swissChilicc27cfe2020-08-08 12:57:57 -07007#ifndef NO_PTHREAD
8#include <pthread.h>
9#endif
10
swissChilibb478f12020-08-07 20:45:07 -070011struct nk_color byte_to_color(uint8_t b)
12{
13 struct nk_color c;
swissChili94ba1f52020-08-08 11:39:10 -070014 c.r = (b >> 5) * (255 / 0b111);
swissChilibb478f12020-08-07 20:45:07 -070015 c.g = ((b >> 2) & 0b111) * (255 / 0b111);
16 c.b = (b & 0b11) * (255 / 0b11);
17 c.a = 255;
18 return c;
19}
20
21void screen(struct nk_context *ctx, uint8_t *mem, uint8_t size)
22{
23 struct nk_command_buffer *out = nk_window_get_canvas(ctx);
24
25 struct nk_rect bounds;
26 enum nk_widget_layout_states state = nk_widget(&bounds, ctx);
27
28 if (!state)
29 return;
30
31 //nk_fill_rect(out, bounds, 0, nk_rgb(255, 0, 0));
32
33 //return;
34
35 for (int i = 0; i < CPU_FB_H; i++)
36 {
37 for (int j = 0; j < CPU_FB_W; j++)
38 {
39 nk_fill_rect(out,
40 nk_rect(bounds.x + i * size, bounds.y + j * size,
41 size, size), 0.0f,
swissChili94ba1f52020-08-08 11:39:10 -070042 byte_to_color(mem[i + CPU_FB_H * j]));
swissChilibb478f12020-08-07 20:45:07 -070043 }
44 }
45}
swissChili94ba1f52020-08-08 11:39:10 -070046
47sdl_screen_t new_sdl_screen(uint8_t size)
48{
49 sdl_screen_t scr;
50 scr.win = SDL_CreateWindow("6502",
51 SDL_WINDOWPOS_CENTERED,
52 SDL_WINDOWPOS_CENTERED,
53 size * 32,
54 size * 32,
55 0);
swissChilib71e0272020-08-08 15:56:14 -070056 ASSERT("Create Screen SDL_Window", scr.win);
swissChili94ba1f52020-08-08 11:39:10 -070057 scr.size = size;
58 scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
swissChilib71e0272020-08-08 15:56:14 -070059 ASSERT("Create SDL_Renderer", scr.r);
swissChilicc27cfe2020-08-08 12:57:57 -070060 scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
61 CPU_FB_W, CPU_FB_H);
swissChilib71e0272020-08-08 15:56:14 -070062 ASSERT("Create SDL_Texture", scr.tex);
swissChili94ba1f52020-08-08 11:39:10 -070063
64 return scr;
65}
66
swissChilicc27cfe2020-08-08 12:57:57 -070067void free_sdl_screen(sdl_screen_t *scr)
swissChili94ba1f52020-08-08 11:39:10 -070068{
swissChilicc27cfe2020-08-08 12:57:57 -070069 //free(scr->fb);
70 SDL_DestroyTexture(scr->tex);
71 SDL_DestroyRenderer(scr->r);
72 SDL_DestroyWindow(scr->win);
73}
74
75bool sdl_screen(sdl_screen_t *scr, uint8_t *mem, bool dirty)
76{
77 static bool texture_set = false;
78 if (!texture_set)
79 {
80 SDL_UpdateTexture(scr->tex, NULL, mem, CPU_FB_W);
81 }
swissChili94ba1f52020-08-08 11:39:10 -070082
83 SDL_Event e;
84
85 while (SDL_PollEvent(&e))
86 {
87 switch (e.type)
88 {
89 case SDL_QUIT:
swissChilicc27cfe2020-08-08 12:57:57 -070090 return true;
swissChili94ba1f52020-08-08 11:39:10 -070091 }
92 }
93
swissChilicc27cfe2020-08-08 12:57:57 -070094 if (dirty)
swissChili94ba1f52020-08-08 11:39:10 -070095 {
swissChilicc27cfe2020-08-08 12:57:57 -070096 SDL_RenderClear(scr->r);
97 SDL_RenderCopy(scr->r, scr->tex, NULL, NULL);
98 SDL_RenderPresent(scr->r);
swissChili94ba1f52020-08-08 11:39:10 -070099 }
100
swissChilicc27cfe2020-08-08 12:57:57 -0700101 return false;
swissChili94ba1f52020-08-08 11:39:10 -0700102}
swissChilicc27cfe2020-08-08 12:57:57 -0700103
104
105#ifndef NO_PTHREAD
106
swissChilib71e0272020-08-08 15:56:14 -0700107bool g_screen_thread_halt = false;
108
109
swissChilicc27cfe2020-08-08 12:57:57 -0700110void *screen_thread(uint8_t *mem)
111{
112 sdl_screen_t scr = new_sdl_screen(8);
113 while (true)
114 {
swissChilib71e0272020-08-08 15:56:14 -0700115 if (sdl_screen(&scr, mem, true) || g_screen_thread_halt)
swissChilicc27cfe2020-08-08 12:57:57 -0700116 break;
117 }
118 free_sdl_screen(&scr);
119
120 exit(0);
121
122 return NULL;
123}
124
swissChilib71e0272020-08-08 15:56:14 -0700125pthread_t start_screen_thread(uint8_t *mem)
swissChilicc27cfe2020-08-08 12:57:57 -0700126{
127 pthread_t thread;
128 pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem);
swissChilib71e0272020-08-08 15:56:14 -0700129 return thread;
swissChilicc27cfe2020-08-08 12:57:57 -0700130}
131
132#endif