blob: 4b01adc4ecffb8b58481a6a8e967d7c6240dadd9 [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
29 //nk_fill_rect(out, bounds, 0, nk_rgb(255, 0, 0));
30
31 //return;
32
33 for (int i = 0; i < CPU_FB_H; i++)
34 {
35 for (int j = 0; j < CPU_FB_W; j++)
36 {
37 nk_fill_rect(out,
38 nk_rect(bounds.x + i * size, bounds.y + j * size,
39 size, size), 0.0f,
swissChili94ba1f52020-08-08 11:39:10 -070040 byte_to_color(mem[i + CPU_FB_H * j]));
swissChilibb478f12020-08-07 20:45:07 -070041 }
42 }
43}
swissChili94ba1f52020-08-08 11:39:10 -070044
45sdl_screen_t new_sdl_screen(uint8_t size)
46{
47 sdl_screen_t scr;
48 scr.win = SDL_CreateWindow("6502",
49 SDL_WINDOWPOS_CENTERED,
50 SDL_WINDOWPOS_CENTERED,
51 size * 32,
52 size * 32,
53 0);
swissChilib71e0272020-08-08 15:56:14 -070054 ASSERT("Create Screen SDL_Window", scr.win);
swissChili94ba1f52020-08-08 11:39:10 -070055 scr.size = size;
56 scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
swissChilib71e0272020-08-08 15:56:14 -070057 ASSERT("Create SDL_Renderer", scr.r);
swissChilicc27cfe2020-08-08 12:57:57 -070058 scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
59 CPU_FB_W, CPU_FB_H);
swissChilib71e0272020-08-08 15:56:14 -070060 ASSERT("Create SDL_Texture", scr.tex);
swissChili94ba1f52020-08-08 11:39:10 -070061
62 return scr;
63}
64
swissChilicc27cfe2020-08-08 12:57:57 -070065void free_sdl_screen(sdl_screen_t *scr)
swissChili94ba1f52020-08-08 11:39:10 -070066{
swissChilicc27cfe2020-08-08 12:57:57 -070067 //free(scr->fb);
68 SDL_DestroyTexture(scr->tex);
69 SDL_DestroyRenderer(scr->r);
70 SDL_DestroyWindow(scr->win);
71}
72
73bool sdl_screen(sdl_screen_t *scr, uint8_t *mem, bool dirty)
74{
75 static bool texture_set = false;
76 if (!texture_set)
77 {
78 SDL_UpdateTexture(scr->tex, NULL, mem, CPU_FB_W);
79 }
swissChili94ba1f52020-08-08 11:39:10 -070080
81 SDL_Event e;
82
83 while (SDL_PollEvent(&e))
84 {
85 switch (e.type)
86 {
87 case SDL_QUIT:
swissChilicc27cfe2020-08-08 12:57:57 -070088 return true;
swissChili94ba1f52020-08-08 11:39:10 -070089 }
90 }
91
swissChilicc27cfe2020-08-08 12:57:57 -070092 if (dirty)
swissChili94ba1f52020-08-08 11:39:10 -070093 {
swissChilicc27cfe2020-08-08 12:57:57 -070094 SDL_RenderClear(scr->r);
95 SDL_RenderCopy(scr->r, scr->tex, NULL, NULL);
96 SDL_RenderPresent(scr->r);
swissChili94ba1f52020-08-08 11:39:10 -070097 }
98
swissChilicc27cfe2020-08-08 12:57:57 -070099 return false;
swissChili94ba1f52020-08-08 11:39:10 -0700100}
swissChilicc27cfe2020-08-08 12:57:57 -0700101
swissChilib71e0272020-08-08 15:56:14 -0700102bool g_screen_thread_halt = false;
103
swissChilicc27cfe2020-08-08 12:57:57 -0700104void *screen_thread(uint8_t *mem)
105{
106 sdl_screen_t scr = new_sdl_screen(8);
swissChilic6b4f7e2020-08-09 16:36:36 -0700107
108 pthread_cleanup_push(((void (*)(void *))&free_sdl_screen), ((void *)&scr));
109
swissChilicc27cfe2020-08-08 12:57:57 -0700110 while (true)
111 {
swissChilib71e0272020-08-08 15:56:14 -0700112 if (sdl_screen(&scr, mem, true) || g_screen_thread_halt)
swissChilicc27cfe2020-08-08 12:57:57 -0700113 break;
114 }
swissChilicc27cfe2020-08-08 12:57:57 -0700115
swissChilic6b4f7e2020-08-09 16:36:36 -0700116 pthread_cleanup_pop(true);
swissChilicc27cfe2020-08-08 12:57:57 -0700117
swissChilic6b4f7e2020-08-09 16:36:36 -0700118 pthread_exit(NULL);
swissChilicc27cfe2020-08-08 12:57:57 -0700119}
120
swissChilib71e0272020-08-08 15:56:14 -0700121pthread_t start_screen_thread(uint8_t *mem)
swissChilicc27cfe2020-08-08 12:57:57 -0700122{
123 pthread_t thread;
124 pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem);
swissChilib71e0272020-08-08 15:56:14 -0700125 return thread;
swissChilicc27cfe2020-08-08 12:57:57 -0700126}