swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 1 | #include "screen.h" |
| 2 | #include "cpu.h" |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 3 | #include "common.h" |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 4 | |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 5 | #include <SDL2/SDL.h> |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 6 | #include <pthread.h> |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 7 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 8 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 9 | struct nk_color byte_to_color(uint8_t b) |
| 10 | { |
| 11 | struct nk_color c; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 12 | c.r = (b >> 5) * (255 / 0b111); |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 13 | c.g = ((b >> 2) & 0b111) * (255 / 0b111); |
| 14 | c.b = (b & 0b11) * (255 / 0b11); |
| 15 | c.a = 255; |
| 16 | return c; |
| 17 | } |
| 18 | |
| 19 | void 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 | |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 29 | nk_fill_rect(out, bounds, 0, nk_rgb(0, 0, 0)); |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 30 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 31 | |
| 32 | for (int i = 0; i < CPU_FB_H; i++) |
| 33 | { |
| 34 | for (int j = 0; j < CPU_FB_W; j++) |
| 35 | { |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 36 | 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 | } |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 46 | |
| 47 | sdl_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); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 56 | ASSERT("Create Screen SDL_Window", scr.win); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 57 | scr.size = size; |
| 58 | scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 59 | ASSERT("Create SDL_Renderer", scr.r); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 60 | scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, |
| 61 | CPU_FB_W, CPU_FB_H); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 62 | ASSERT("Create SDL_Texture", scr.tex); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 63 | |
| 64 | return scr; |
| 65 | } |
| 66 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 67 | void free_sdl_screen(sdl_screen_t *scr) |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 68 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 69 | //free(scr->fb); |
| 70 | SDL_DestroyTexture(scr->tex); |
| 71 | SDL_DestroyRenderer(scr->r); |
| 72 | SDL_DestroyWindow(scr->win); |
| 73 | } |
| 74 | |
| 75 | bool 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 | } |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 82 | |
| 83 | SDL_Event e; |
| 84 | |
| 85 | while (SDL_PollEvent(&e)) |
| 86 | { |
| 87 | switch (e.type) |
| 88 | { |
| 89 | case SDL_QUIT: |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 90 | return true; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 94 | if (dirty) |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 95 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 96 | SDL_RenderClear(scr->r); |
| 97 | SDL_RenderCopy(scr->r, scr->tex, NULL, NULL); |
| 98 | SDL_RenderPresent(scr->r); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 99 | } |
| 100 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 101 | return false; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 102 | } |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 103 | |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 104 | bool g_screen_thread_halt = false; |
| 105 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 106 | void *screen_thread(uint8_t *mem) |
| 107 | { |
| 108 | sdl_screen_t scr = new_sdl_screen(8); |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 109 | |
| 110 | pthread_cleanup_push(((void (*)(void *))&free_sdl_screen), ((void *)&scr)); |
| 111 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 112 | while (true) |
| 113 | { |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 114 | if (sdl_screen(&scr, mem, true) || g_screen_thread_halt) |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 115 | break; |
| 116 | } |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 117 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 118 | pthread_cleanup_pop(true); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 119 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 120 | pthread_exit(NULL); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 121 | } |
| 122 | |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 123 | pthread_t start_screen_thread(uint8_t *mem) |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 124 | { |
| 125 | pthread_t thread; |
| 126 | pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem); |
swissChili | b71e027 | 2020-08-08 15:56:14 -0700 | [diff] [blame] | 127 | return thread; |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame] | 128 | } |