swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 1 | #include "screen.h" |
| 2 | #include "cpu.h" |
| 3 | |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 4 | #include <SDL2/SDL.h> |
| 5 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 6 | #ifndef NO_PTHREAD |
| 7 | #include <pthread.h> |
| 8 | #endif |
| 9 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 10 | struct nk_color byte_to_color(uint8_t b) |
| 11 | { |
| 12 | struct nk_color c; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 13 | c.r = (b >> 5) * (255 / 0b111); |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 14 | c.g = ((b >> 2) & 0b111) * (255 / 0b111); |
| 15 | c.b = (b & 0b11) * (255 / 0b11); |
| 16 | c.a = 255; |
| 17 | return c; |
| 18 | } |
| 19 | |
| 20 | void 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, |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 41 | byte_to_color(mem[i + CPU_FB_H * j])); |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | } |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 45 | |
| 46 | sdl_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); |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 57 | scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING, |
| 58 | CPU_FB_W, CPU_FB_H); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 59 | |
| 60 | return scr; |
| 61 | } |
| 62 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 63 | void free_sdl_screen(sdl_screen_t *scr) |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 64 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 65 | //free(scr->fb); |
| 66 | SDL_DestroyTexture(scr->tex); |
| 67 | SDL_DestroyRenderer(scr->r); |
| 68 | SDL_DestroyWindow(scr->win); |
| 69 | } |
| 70 | |
| 71 | bool 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 | } |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 78 | |
| 79 | SDL_Event e; |
| 80 | |
| 81 | while (SDL_PollEvent(&e)) |
| 82 | { |
| 83 | switch (e.type) |
| 84 | { |
| 85 | case SDL_QUIT: |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 86 | return true; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 90 | if (dirty) |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 91 | { |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 92 | SDL_RenderClear(scr->r); |
| 93 | SDL_RenderCopy(scr->r, scr->tex, NULL, NULL); |
| 94 | SDL_RenderPresent(scr->r); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 95 | } |
| 96 | |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 97 | return false; |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 98 | } |
swissChili | cc27cfe | 2020-08-08 12:57:57 -0700 | [diff] [blame^] | 99 | |
| 100 | |
| 101 | #ifndef NO_PTHREAD |
| 102 | |
| 103 | void *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 | |
| 118 | void 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 |