Skip to content

Commit

Permalink
[cpp] screenshot support
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Apr 6, 2024
1 parent cf73322 commit 4605e5f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions cpp/src/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Args::Args(int argc, char *argv[]) {
args::Flag debug_ram(parser, "debug-ram", "Debug RAM", {'a', "debug-ram"});
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::Flag version(parser, "version", "Show build info", {'v', "version"});
args::Positional<std::string> rom(parser, "rom", "Path to a .gb file");
Expand Down Expand Up @@ -48,6 +49,7 @@ Args::Args(int argc, char *argv[]) {
this->debug_ram = debug_ram;
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 @@ -15,6 +15,7 @@ class Args {
bool debug_ram;
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, args->debug_ram);
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
9 changes: 7 additions & 2 deletions cpp/src/gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,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 @@ -62,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 Expand Up @@ -355,4 +360,4 @@ SDL_Color gen_hue(u8 n) {
}
}

bool Sprite::is_live() { return x > 0 && x < 168 && y > 0 && y < 160; }
bool Sprite::is_live() { return x > 0 && x < 168 && y > 0 && y < 160; }
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 4605e5f

Please sign in to comment.