-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
155 lines (135 loc) · 4.22 KB
/
test.cpp
File metadata and controls
155 lines (135 loc) · 4.22 KB
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
#include <assert.h>
#include <stdint.h>
#include <SDL.h>
#include <iostream>
#include <fstream>
typedef int8_t int8;
typedef uint8_t uint8;
typedef uint16_t uint16;
#include "data-interop.hpp"
const int ImgW = 512;
const int ImgH = 480;
const int WindowScale = 1;
extern "C" {
bool libsfcppu_init();
uint16 const * libsfcppu_drawFrame(SnesFrameData const * ppuState);
}
void drawFrame(byte* buffer, int pitch, const SnesFrameData* d) {
assert(ImgW == 512);
assert(ImgH == 480);
assert(pitch == ImgW * 4);
ushort const * rawdata = libsfcppu_drawFrame(d);
for (int y = 0; y < ImgH; y += 1) {
byte* pxptr = buffer + pitch * y;
for (int x = 0; x < ImgW; x += 1) {
ushort px = rawdata[y * 512 + x];
/* R */ pxptr[0] = ((px >> 0) & 31) << 3;
/* G */ pxptr[1] = ((px >> 5) & 31) << 3;
/* B */ pxptr[2] = ((px >> 10) & 31) << 3;
pxptr += 4;
}
}
}
extern "C" int SDL_main(int argc, char ** argv) {
std::ofstream logger("log.txt", std::ios::trunc);
logger << "Hello world!" << std::endl;
if(!libsfcppu_init()) {
logger << "Error initializing DrawFrame!" << std::endl;
return 1;
}
if(SDL_Init(SDL_INIT_VIDEO) != 0) {
logger << "Error initializing SDL: " << SDL_GetError() << std::endl;
return 1;
}
const int windowFlags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
SDL_Window* appWin = SDL_CreateWindow(
"SDL Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
ImgW * WindowScale,
ImgH * WindowScale,
windowFlags
);
if(!appWin) {
logger << "Error creating SDL window: " << SDL_GetError() << std::endl;
return 1;
}
const int rendererFlags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
SDL_Renderer* renderer = SDL_CreateRenderer(
appWin, -1, rendererFlags
);
if(!renderer) {
logger << "Error creating SDL renderer: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Texture* drawTexture = SDL_CreateTexture(
renderer,
SDL_PIXELFORMAT_BGR888,
SDL_TEXTUREACCESS_STREAMING,
ImgW,
ImgH
);
if(!drawTexture) {
logger << "Error creating SDL texture: " << SDL_GetError() << std::endl;
return 1;
}
bool run = true, pause = false;
SnesFrameData frameData = {};
frameData.INIDISP = 0x0f;
frameData.BGMODE = 1;
frameData.MOSAIC = 0;
frameData.TM = 0;
frameData.TS = 0;
frameData.TMW = 0;
frameData.TSW = 0;
frameData.cgram[0] = 0x1fff;
{
int i;
for (i = 0; i < 16; i += 1) {
frameData.hdmaData[i].vcounter = i + 10;
frameData.hdmaData[i].addr = 0x00;
frameData.hdmaData[i].value = 15 - i;
}
for (; i < 32; i += 1) {
frameData.hdmaData[i].vcounter = i + 10;
frameData.hdmaData[i].addr = 0x00;
frameData.hdmaData[i].value = i - 16;
}
frameData.numHdmaWrites = i;
}
logger << "Beginning SDL loop..." << std::endl;
int lastTime;
while(run) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
run = false;
}
if (event.type == SDL_KEYDOWN) {
pause = !pause;
}
}
lastTime = SDL_GetTicks();
byte* drawBuffer;
int drawPitch;
SDL_LockTexture(drawTexture, NULL, (void**)&drawBuffer, &drawPitch);
drawFrame(drawBuffer, drawPitch, &frameData);
SDL_UnlockTexture(drawTexture);
SDL_SetRenderDrawColor(renderer, 120, 140, 230, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, drawTexture, NULL, NULL);
SDL_RenderPresent(renderer);
int drawTime = SDL_GetTicks() - lastTime;
if(drawTime < 16) {
SDL_Delay(16 - drawTime);
}
}
// Close and destroy the texture
if(drawTexture) SDL_DestroyTexture(drawTexture);
// Close and destroy the renderer
if(renderer) SDL_DestroyRenderer(renderer);
// Close and destroy the window
if(appWin) SDL_DestroyWindow(appWin);
SDL_Quit();
return 0;
}