Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cpp] screenshot support #52

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading