~swisschili/toolchain-6502

toolchain-6502/screen.c -rw-r--r-- 2.4 KiB
a091d887swissChili Readme change to reflect status of assembler 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "screen.h"
#include "cpu.h"
#include "common.h"

#include <SDL2/SDL.h>
#include <pthread.h>


struct nk_color byte_to_color(uint8_t b)
{
	struct nk_color c;
	c.r = (b >> 5) * (255 / 0b111);
	c.g = ((b >> 2) & 0b111) * (255 / 0b111);
	c.b = (b & 0b11) * (255 / 0b11);
	c.a = 255;
	return c;
}

void screen(struct nk_context *ctx, uint8_t *mem, uint8_t size)
{
	struct nk_command_buffer *out = nk_window_get_canvas(ctx);

	struct nk_rect bounds;
	enum nk_widget_layout_states state = nk_widget(&bounds, ctx);

	if (!state)
		return;

	nk_fill_rect(out, bounds, 0, nk_rgb(0, 0, 0));


	for (int i = 0; i < CPU_FB_H; i++)
	{
		for (int j = 0; j < CPU_FB_W; j++)
		{
			if (mem[i + CPU_FB_H * j])
			{
				nk_fill_rect(out,
					nk_rect(bounds.x + i * size, bounds.y + j * size,
						size, size), 0.0f,
					byte_to_color(mem[i + CPU_FB_H * j]));
			}
		}
	}
}

sdl_screen_t new_sdl_screen(uint8_t size)
{
	sdl_screen_t scr;
	scr.win = SDL_CreateWindow("6502",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		size * 32,
		size * 32,
		0);
	ASSERT("Create Screen SDL_Window", scr.win);
	scr.size = size;
	scr.r = SDL_CreateRenderer(scr.win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	ASSERT("Create SDL_Renderer", scr.r);
	scr.tex = SDL_CreateTexture(scr.r, SDL_PIXELFORMAT_RGB332, SDL_TEXTUREACCESS_STREAMING,
		CPU_FB_W, CPU_FB_H);
	ASSERT("Create SDL_Texture", scr.tex);

	return scr;
}

void free_sdl_screen(sdl_screen_t *scr)
{
	puts("Freed SDL Screen");
	SDL_DestroyTexture(scr->tex);
	SDL_DestroyRenderer(scr->r);
	SDL_DestroyWindow(scr->win);
}

bool sdl_screen(sdl_screen_t *scr, uint8_t *mem, bool dirty)
{
	static bool texture_set = false;
	if (!texture_set)
	{
		SDL_UpdateTexture(scr->tex, NULL, mem, CPU_FB_W);
	}

	SDL_Event e;

	while (SDL_PollEvent(&e))
	{
		switch (e.type)
		{
			case SDL_QUIT:
				return true;
		}
	}

	if (dirty)
	{
		SDL_RenderClear(scr->r);
		SDL_RenderCopy(scr->r, scr->tex, NULL, NULL);
		SDL_RenderPresent(scr->r);	
	}

	return false;
}

bool g_screen_thread_halt = false;

void *screen_thread(uint8_t *mem)
{
	sdl_screen_t scr = new_sdl_screen(8);
	
	pthread_cleanup_push(((void (*)(void *))&free_sdl_screen), ((void *)&scr));

	while (true)
	{
		if (sdl_screen(&scr, mem, true) || g_screen_thread_halt)
			break;
	}

	pthread_cleanup_pop(true);

	pthread_exit(NULL);
}

pthread_t start_screen_thread(uint8_t *mem)
{
	pthread_t thread;
	pthread_create(&thread, NULL, (void *(*)(void *))&screen_thread, mem);
	return thread;
}