Skip to content

Commit 92556cd

Browse files
committed
Improve how pixels are rendered to the screen
1 parent f214f7f commit 92556cd

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

src/sdl/Emulator.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ Emulator::Emulator(const std::string& path, const RomSettings& romSettings, cons
1414
if (this->path.ends_with(".state")) {
1515
std::ifstream fileReader;
1616
fileReader.open(path, std::ios::binary);
17-
17+
1818
if (!fileReader.is_open()) {
1919
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error opening file '%s'", path.c_str());
2020
return;
2121
}
2222

2323
std::vector<uint8_t> buffer(sha1Dimension);
24-
24+
2525
fileReader.seekg(0, std::ios::end);
2626
size_t file_size = fileReader.tellg();
2727
fileReader.seekg(Cpu::serializationDimension - 1, std::ios::beg);
28-
28+
2929
fileReader.read(reinterpret_cast<char*>(&buffer[0]), sha1Dimension);
3030
fileReader.close();
3131

@@ -45,6 +45,14 @@ void Emulator::init() {
4545
Window::init(64 * 15, 32 * 15);
4646
#endif
4747

48+
this->renderTexture = SDL_CreateTexture(
49+
this->renderer,
50+
SDL_PIXELFORMAT_RGBA8888,
51+
SDL_TEXTUREACCESS_STREAMING,
52+
64, 32
53+
);
54+
SDL_SetTextureScaleMode(this->renderTexture, SDL_SCALEMODE_NEAREST);
55+
4856
if (this->path.empty()) {
4957
std::cerr << "No ROM file has been specified :(" << std::endl;
5058
return;
@@ -101,19 +109,24 @@ void Emulator::update() {
101109
}
102110

103111
void Emulator::render() {
104-
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 0);
105-
SDL_RenderClear(this->renderer);
112+
static uint32_t pixels[64 * 32];
113+
const auto& display = this->cpu.getDisplay();
114+
for (int i = 0; i < 64 * 32; ++i) {
115+
pixels[i] = 0x00FFFFFF * display[i] | 0xFF000000;
116+
}
106117

107-
SDL_SetRenderDrawColor(this->renderer, 255, 255, 255, 255);
118+
SDL_UpdateTexture(this->renderTexture, nullptr, pixels, 64 * sizeof(uint32_t));
108119

109-
for (uint8_t y = 0; y < 32; ++y) {
110-
for (uint8_t x = 0; x < 64; ++x) {
111-
if (this->cpu.getDisplay()[y * 64 + x]) {
112-
const SDL_FRect rect = {this->offsetX + static_cast<float>(x) * this->scale, this->offsetY + static_cast<float>(y) * this->scale, this->scale, this->scale};
113-
SDL_RenderFillRect(renderer, &rect);
114-
}
115-
}
116-
}
120+
const SDL_FRect rect = {
121+
static_cast<float>(this->offsetX),
122+
static_cast<float>(this->offsetY),
123+
64 * this->scale,
124+
32 * this->scale
125+
};
126+
127+
SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255);
128+
SDL_RenderClear(this->renderer);
129+
SDL_RenderTexture(this->renderer, this->renderTexture, nullptr, &rect);
117130
SDL_RenderPresent(this->renderer);
118131
}
119132

@@ -134,7 +147,7 @@ void Emulator::handleSaveState() {
134147
SDL_ShowSaveFileDialog([](void* userData, const char* const* selectedPath, int filter) {
135148
const auto data = static_cast<Emulator*>(userData);
136149
data->isStopped = false;
137-
150+
138151
if (!selectedPath || !*selectedPath) {
139152
SDL_Log("The user did not select any file. Most likely, the dialog was canceled.");
140153
return;
@@ -153,19 +166,19 @@ void Emulator::saveState(const std::string& path) {
153166
std::ofstream fileWriter;
154167
fileWriter.open(path, std::ios::binary);
155168
auto serialization = this->cpu.serialize();
156-
169+
157170
// After the CPU serialization, I insert the SHA1 of the ROM to check it when selecting a state
158171
// in the main dialog
159172
serialization.insert(serialization.end(), this->sha1.cbegin(), this->sha1.cend());
160-
173+
161174
fileWriter.write(reinterpret_cast<char*>(serialization.data()), serialization.size());
162175
fileWriter.close();
163176
}
164177

165178
void Emulator::loadState() {
166179
std::ifstream fileReader;
167180
fileReader.open(this->path, std::ios::binary);
168-
181+
169182
if (!fileReader.is_open()) {
170183
std::cerr << "Error opening file '" << this->path << "'" << std::endl;
171184
return;
@@ -176,7 +189,7 @@ void Emulator::loadState() {
176189
fileReader.seekg(0, std::ios::end);
177190
const size_t file_size = fileReader.tellg();
178191
fileReader.seekg(0, std::ios::beg);
179-
192+
180193
fileReader.read(reinterpret_cast<char*>(&buffer[0]), file_size);
181194
fileReader.close();
182195

src/sdl/Emulator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
class Emulator : public Window {
1212
private:
13+
SDL_Texture* renderTexture;
1314
Speaker speaker;
1415
Keyboard keyboard;
1516
Cpu cpu;

0 commit comments

Comments
 (0)