Skip to content

Commit

Permalink
[cpp] screenshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Nov 18, 2022
1 parent f78b607 commit c08ff64
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cpp/src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Args::Args(int argc, char *argv[]) {
args::Flag debug_apu(parser, "debug-apu", "Debug APU", {'a', "debug-apu"});
args::ValueFlag<int> frames(parser, "frames", "Exit after N frames", {'f', "frames"});
args::ValueFlag<int> profile(parser, "profile", "Exit after N seconds", {'p', "profile"});
args::ValueFlag<std::string> screenshot(parser, "screenshot", "Write a BMP to this file on exit", {'s', "screenshot"});
args::Flag turbo(parser, "turbo", "No sleep between frames", {'t', "turbo"});
args::Positional<std::string> rom(parser, "rom", "Path to a .gb file");
args::CompletionFlag completion(parser, {"complete"});
Expand Down Expand Up @@ -39,6 +40,7 @@ Args::Args(int argc, char *argv[]) {
this->debug_gpu = debug_gpu;
this->frames = frames ? args::get(frames) : 0;
this->profile = profile ? args::get(profile) : 0;
this->screenshot = screenshot ? args::get(screenshot) : "";
this->turbo = turbo;
this->rom = args::get(rom);
}
1 change: 1 addition & 0 deletions cpp/src/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Args {
bool debug_apu;
int frames;
int profile;
std::string screenshot;
bool turbo;
std::string rom;
};
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/gameboy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ GameBoy::GameBoy(Args *args) {
this->cart = new Cart(args->rom);
this->ram = new RAM(this->cart);
this->cpu = new CPU(this->ram, args->debug_cpu);
this->gpu = new GPU(this->cpu, cart->name, args->headless, args->debug_gpu);
this->gpu = new GPU(this->cpu, cart->name, args->screenshot, args->headless, args->debug_gpu);
this->buttons = new Buttons(this->cpu, args->headless);
if(!args->silent) new APU(this->cpu, args->debug_apu);
this->clock = new Clock(this->buttons, args->frames, args->profile, args->turbo);
Expand Down
8 changes: 7 additions & 1 deletion cpp/src/gpu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <SDL2/SDL.h>
#include <iostream>

#include "consts.h"
#include "gpu.h"
Expand All @@ -17,7 +18,8 @@ u32 bmask = 0x00ff0000;
u32 amask = 0xff000000;
#endif

GPU::GPU(CPU *cpu, char *title, bool headless, bool debug) {
GPU::GPU(CPU *cpu, char *title, std::string screenshot, bool headless, bool debug) {
this->screenshot = screenshot;
this->cpu = cpu;
this->debug = debug;

Expand Down Expand Up @@ -61,6 +63,10 @@ GPU::GPU(CPU *cpu, char *title, bool headless, bool debug) {
};

GPU::~GPU() {
std::cout << "Screenshot " << this->screenshot << "\n";
if(this->screenshot.length() > 0) {
SDL_SaveBMP(this->buffer, this->screenshot.c_str());
}
SDL_FreeSurface(this->buffer);
if(this->hw_window) SDL_DestroyWindow(this->hw_window);
SDL_Quit();
Expand Down
3 changes: 2 additions & 1 deletion cpp/src/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ struct Sprite {
class GPU {
private:
bool debug;
std::string screenshot;
SDL_Window *hw_window;
SDL_Texture *hw_buffer;
SDL_Renderer *hw_renderer;
Expand All @@ -62,7 +63,7 @@ class GPU {
CPU *cpu;

public:
GPU(CPU *cpu, char *title, bool headless, bool debug);
GPU(CPU *cpu, char *title, std::string screenshot, bool headless, bool debug);
~GPU();
void tick();

Expand Down

0 comments on commit c08ff64

Please sign in to comment.