forked from EtchedPixels/EmulatorKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6847_sdl2.c
123 lines (107 loc) · 3.28 KB
/
6847_sdl2.c
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
/*
* 6847 driver raster output for SDL2
*
* There are lots of variants of this device that have subtle differences
* including NTSC and PAL versions.
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>
#include "6847.h"
#include "6847_render.h"
extern int sdl_live;
/*
* The 6847 set up is odd - there are 8 colours in the encoding plus black
* which has a life of its own and we keep in slot 8
*
* Ignore colour artifacting for the moment
*
* These colours seem to be very subjective and system dependent and
* in reality also shift a bit by mode and PAL/NTSC.
*/
static uint32_t vdp_ctab[9] = {
0xFF30D200, /* Green */
0xFFC1E500, /* Yellow */
0xFF4C3AB4, /* Blue */
0xFF9A3236, /* Red */
0xFFBFC8AD, /* "Buff" */
0xFF41AF71, /* Cyan */
0xFFC84EF0, /* Magenta */
0xFFD47F00, /* Orange/Brown */
0xFF263016, /* Black */
};
struct m6847_renderer {
struct m6847 *vdp;
SDL_Renderer *render;
SDL_Texture *texture;
SDL_Window *window;
};
void m6847_render(struct m6847_renderer *render)
{
SDL_Rect sr;
sr.x = 0;
sr.y = 0;
sr.w = 256;
sr.h = 192;
SDL_UpdateTexture(render->texture, NULL, m6847_get_raster(render->vdp), 1024);
SDL_RenderClear(render->render);
SDL_RenderCopy(render->render, render->texture, NULL, &sr);
SDL_RenderPresent(render->render);
}
void m6847_renderer_free(struct m6847_renderer *render)
{
if (render->texture)
SDL_DestroyTexture(render->texture);
}
struct m6847_renderer *m6847_renderer_create(struct m6847 *vdp)
{
struct m6847_renderer *render;
/* We will need a nicer way to do this once we have multiple SDL using
devices */
if (sdl_live == 0) {
sdl_live = 1;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr, "SDL init failed: %s.\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
}
render = malloc(sizeof(struct m6847_renderer));
if (render == NULL) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
memset(render, 0, sizeof(struct m6847_renderer));
render->vdp = vdp;
m6847_set_colourmap(vdp, vdp_ctab);
render->window = SDL_CreateWindow("6847",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
512, 384,
SDL_WINDOW_RESIZABLE);
if (render->window == NULL) {
fprintf(stderr, "Unable to create window: %s.\n", SDL_GetError());
exit(1);
}
render->render = SDL_CreateRenderer(render->window, -1, SDL_RENDERER_ACCELERATED);
if (render->render == NULL) {
fprintf(stderr, "Unable to create renderer: %s.\n", SDL_GetError());
exit(1);
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
SDL_RenderSetLogicalSize(render->render, 256, 192);
render->texture = SDL_CreateTexture(render->render,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
256, 192);
if (render->render == NULL) {
fprintf(stderr, "Unable to create renderer: %s.\n", SDL_GetError());
exit(1);
}
SDL_SetRenderDrawColor(render->render, 0, 0, 0, 255);
SDL_RenderClear(render->render);
SDL_RenderPresent(render->render);
return render;
}