blob: 7d09daf27e747ad9d44b63ecaba004afe22ab002 [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>
swissChilicc27cfe2020-08-08 12:57:57 -07006#include <pthread.h>
swissChilic6b4f7e2020-08-09 16:36:36 -07007
swissChilicc27cfe2020-08-08 12:57:57 -07008
swissChilibb478f12020-08-07 20:45:07 -07009struct nk_color byte_to_color(uint8_t b)
10{
11 struct nk_color c;
swissChili94ba1f52020-08-08 11:39:10 -070012 c.r = (b >> 5) * (255 / 0b111);
swissChilibb478f12020-08-07 20:45:07 -070013 c.g = ((b >> 2) & 0b111) * (255 / 0b111);
14 c.b = (b & 0b11) * (255 / 0b11);
15 c.a = 255;
16 return c;
17}
18
19void screen(struct nk_context *ctx, uint8_t *mem, uint8_t size)
20{
21 struct nk_command_buffer *out = nk_window_get_canvas(ctx);
22
23 struct nk_rect bounds;
24 enum nk_widget_layout_states state = nk_widget(&bounds, ctx);
25
26 if (!state)
27 return;
28
swissChili1970cb82020-08-10 13:22:39 -070029 nk_fill_rect(out, bounds, 0, nk_rgb(0, 0, 0));
swissChilibb478f12020-08-07 20:45:07 -070030
swissChilibb478f12020-08-07 20:45:07 -070031
32 for (int i = 0; i < CPU_FB_H; i++)
33 {
34 for (int j = 0; j < CPU_FB_W; j++)
35 {
swissChili1970cb82020-08-10 13:22:39 -070036 if (mem[i + CPU_FB_H * j])
37 {
38 nk_fill_rect(out,
39 nk_rect(bounds.x + i * size, bounds.y + j * size,
40 size, size), 0.0f,
41 byte_to_color(mem[i + CPU_FB_H * j]));
42 }
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
swissChilib71e0272020-08-08 15:56:14 -0700104bool g_screen_thread_halt = false;
105
swissChilicc27cfe2020-08-08 12:57:57 -0700106void *screen_thread(uint8_t *mem)
107{
108 sdl_screen_t scr = new_sdl_screen(8);
swissChilic6b4f7e2020-08-09 16:36:36 -0700109
110 pthread_cleanup_push(((void (*)(void *))&free_sdl_screen), ((void *)&scr));
111
swissChilicc27cfe2020-08-08 12:57:57 -0700112 while (true)
113 {
swissChilib71e0272020-08-08 15:56:14 -0700114 if (sdl_screen(&scr, mem, true) || g_screen_thread_halt)
swissChilicc27cfe2020-08-08 12:57:57 -0700115 break;
116 }
swissChilicc27cfe2020-08-08 12:57:57 -0700117
swissChilic6b4f7e2020-08-09 16:36:36 -0700118 pthread_cleanup_pop(true);
swissChilicc27cfe2020-08-08 12:57:57 -0700119
swissChilic6b4f7e2020-08-09 16:36:36 -0700120 pthread_exit(NULL);
swissChilicc27cfe2020-08-08 12:57:57 -0700121}
122
swissChilib71e0272020-08-08 15:56:14 -0700123pthread_t start_screen_thread(uint8_t *mem)
swissChilicc27cfe2020-08-08 12:57:57 -0700124{
125 pthread_t thread;
126 pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem);
swissChilib71e0272020-08-08 15:56:14 -0700127 return thread;
swissChilicc27cfe2020-08-08 12:57:57 -0700128}