-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo.c
180 lines (151 loc) · 4.39 KB
/
video.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright (C) 2023 nukeykt
*
* This file is part of Nuked-MD.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* SDL/Video routines
*
*/
#include <stdio.h>
#include <string.h>
#define SDL_MAIN_HANDLED
#include "SDL.h"
#include "video.h"
#include "vdp_sms.h"
extern vdpsms_t vdp;
extern int pal;
extern uint64_t mcycles;
SDL_Window* vid_window;
SDL_Renderer* vid_renderer;
SDL_Texture* vid_texture;
uint32_t vid_counter;
uint32_t vid_counter_write;
uint64_t vid_mcycles;
FILE* vid_dump_file;
uint32_t vid_workbuffer[VID_HEIGHT][VID_WIDTH];
uint32_t vid_currentbuffer[VID_HEIGHT][VID_WIDTH];
uint8_t vid_filebuffer[VID_HEIGHT][VID_WIDTH][3];
SDL_mutex* vid_mutex;
uint64_t frame_mcycles;
void Video_Init(char* videoout_filename)
{
vid_counter = vid_counter_write = 0;
vid_window = SDL_CreateWindow("Nuked SMS", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
VID_WIDTH, VID_HEIGHT * 2, SDL_WINDOW_SHOWN);
if (!vid_window)
return;
vid_renderer = SDL_CreateRenderer(vid_window, -1, 0);
if (!vid_renderer)
return;
vid_texture = SDL_CreateTexture(vid_renderer, SDL_PIXELFORMAT_BGR888, SDL_TEXTUREACCESS_STREAMING,
VID_WIDTH, VID_HEIGHT);
if (!vid_texture)
return;
//vid_dump_file = fopen(videoout_filename, "wb");
vid_mutex = SDL_CreateMutex();
}
void Video_Shutdown(void)
{
if (vid_dump_file) {
fclose(vid_dump_file);
vid_dump_file = NULL;
}
}
void Video_Blit(void)
{
SDL_LockMutex(vid_mutex);
SDL_UpdateTexture(vid_texture, NULL, vid_currentbuffer, VID_WIDTH * 4);
if (vid_dump_file)
{
if (vid_counter_write < vid_counter)
{
int x, y;
for (y = 0; y < VID_HEIGHT; y++)
{
for (x = 0; x < VID_WIDTH; x++)
{
uint32_t abgr = vid_currentbuffer[y][x];
vid_filebuffer[y][x][0] = (abgr >> 0) & 255;
vid_filebuffer[y][x][1] = (abgr >> 8) & 255;
vid_filebuffer[y][x][2] = (abgr >> 16) & 255;
}
}
memcpy(vid_filebuffer, &vid_mcycles, sizeof(vid_mcycles));
fwrite(vid_filebuffer, 1, sizeof(vid_filebuffer), vid_dump_file);
fflush(vid_dump_file);
vid_counter_write = vid_counter;
}
}
SDL_UnlockMutex(vid_mutex);
SDL_RenderCopy(vid_renderer, vid_texture, NULL, NULL);
SDL_RenderPresent(vid_renderer);
}
void Video_PlotVDP(void)
{
static int ocsync;
static int plot_x;
static int plot_y;
static int csync_cnt;
if (!ocsync && vdp.o_csync)
{
if (csync_cnt >= 100)
{
// vsync
plot_y = 0;
if (pal)
plot_y = -22;
SDL_LockMutex(vid_mutex);
memcpy(vid_currentbuffer, vid_workbuffer, sizeof(vid_workbuffer));
vid_mcycles = frame_mcycles;
vid_counter++;
SDL_UnlockMutex(vid_mutex);
memset(vid_workbuffer, 0, sizeof(vid_workbuffer));
frame_mcycles = mcycles;
Video_Blit();
}
else
{
// hsync
plot_x = 0;
plot_y++;
}
csync_cnt = 0;
}
else if (!vdp.o_csync)
{
csync_cnt++;
}
if (plot_x >= 0 && plot_x < VID_WIDTH && plot_y >= 0 && plot_y < VID_HEIGHT/* && vdp.o_csync*/)
{
uint32_t abgr = 0;
abgr |= vdp.o_dac_r << 0;
abgr |= vdp.o_dac_g << 8;
abgr |= vdp.o_dac_b << 16;
vid_workbuffer[plot_y][plot_x] = abgr;
}
plot_x++;
ocsync = vdp.o_csync != 0;
}
void Video_UpdateTitle(uint64_t ms)
{
char buffer[100];
int _ms = ms % 1000;
int _s = (int)(ms / 1000);
int mn = _s / 60;
_s %= 60;
SDL_snprintf(buffer, sizeof(buffer), "Nuked SMS v" VERSION " [%i:%02i:%03i]", mn, _s, _ms);
buffer[99] = 0;
if (!vid_window)
return;
SDL_SetWindowTitle(vid_window, buffer);
}