Skip to content

Commit

Permalink
Merge pull request #826 from Daft-Freak/sdl-window-flags
Browse files Browse the repository at this point in the history
SDL screen mode/size improvements
  • Loading branch information
Gadgetoid committed Jul 5, 2023
2 parents 12ec9e4 + f15cd38 commit fc23c1d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
31 changes: 18 additions & 13 deletions 32blit-sdl/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ void em_loop() {
#endif

int main(int argc, char *argv[]) {
int x, y;
bool custom_window_position = false;
int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
bool fullscreen = false;

std::cout << metadata_title << " " << metadata_version << std::endl;
std::cout << "Powered by 32Blit SDL2 runtime - github.com/32blit/32blit-sdk" << std::endl << std::endl;
Expand All @@ -165,11 +165,19 @@ int main(int argc, char *argv[]) {
else if(arg_str == "--listen")
mp_mode = Multiplayer::Mode::Listen;
else if(arg_str == "--position") {
if(SDL_sscanf(argv[i+1], "%d,%d", &x, &y) == 2) {
custom_window_position = true;
}
}
else if(arg_str == "--credits") {
SDL_sscanf(argv[i+1], "%d,%d", &x, &y);
} else if(arg_str == "--size" && i + 1 < argc) {
int w, h;
if(SDL_sscanf(argv[i+1], "%d,%d", &w, &h) == 2) {
if(w * y < System::max_width * System::max_height) {
System::width = w;
System::height = h;
}
}
i++;
} else if(arg_str == "--fullscreen")
fullscreen = true;
else if(arg_str == "--credits") {
std::cout << "32Blit was made possible by:" << std::endl;
std::cout << std::endl;
for(auto name : contributors) {
Expand Down Expand Up @@ -202,6 +210,7 @@ int main(int argc, char *argv[]) {
std::cout << " --connect <addr> -- Connect to a listening game instance." << std::endl;
std::cout << " --listen -- Listen for incoming connections." << std::endl;
std::cout << " --position x,y -- Set window position." << std::endl;
std::cout << " --size w,h -- Set display size. (max 320x240)" << std::endl;
std::cout << " --launch_path <file> -- Emulates the file associations on the console." << std::endl;
std::cout << " --credits -- Print contributor credits and exit." << std::endl;
std::cout << " --info -- Print metadata info and exit." << std::endl << std::endl;
Expand All @@ -218,9 +227,9 @@ int main(int argc, char *argv[]) {

window = SDL_CreateWindow(
metadata_title,
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
x, y,
System::width*2, System::height*2,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | (fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0)
);

if (window == nullptr) {
Expand All @@ -229,10 +238,6 @@ int main(int argc, char *argv[]) {
}
SDL_SetWindowMinimumSize(window, System::width, System::height);

if(custom_window_position) {
SDL_SetWindowPosition(window, x, y);
}

blit_system = new System();
blit_input = new Input(blit_system);
blit_multiplayer = new Multiplayer(mp_mode, mp_address);
Expand Down
57 changes: 30 additions & 27 deletions 32blit-sdl/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,25 @@

extern Input *blit_input;

int System::width = System::max_width;
int System::height = System::max_height;

// blit framebuffer memory
static uint8_t framebuffer[System::width * System::height * 3];
static uint8_t framebuffer[System::max_width * System::max_height * 3];
static blit::Pen palette[256];

static const blit::SurfaceTemplate __fb_hires{framebuffer, blit::Size(System::width, System::height), blit::PixelFormat::RGB, nullptr};
static const blit::SurfaceTemplate __fb_hires_pal{framebuffer, blit::Size(System::width, System::height), blit::PixelFormat::P, palette};
static const blit::SurfaceTemplate __fb_lores{framebuffer, blit::Size(System::width / 2, System::height / 2), blit::PixelFormat::RGB, nullptr};

// blit debug callback
void blit_debug(const char *message) {
std::cout << message;
}

// blit screenmode callback
blit::ScreenMode _mode = blit::ScreenMode::lores;
static blit::ScreenMode requested_mode = blit::ScreenMode::lores;
static blit::PixelFormat cur_format = blit::PixelFormat::RGB;
static blit::PixelFormat requested_format = blit::PixelFormat::RGB;

blit::SurfaceInfo cur_surf_info;
blit::SurfaceInfo &set_screen_mode(blit::ScreenMode new_mode) {
_mode = new_mode;
switch(_mode) {
case blit::ScreenMode::lores:
cur_surf_info = __fb_lores;
break;
case blit::ScreenMode::hires:
cur_surf_info = __fb_hires;
break;
case blit::ScreenMode::hires_palette:
cur_surf_info = __fb_hires_pal;
break;
}

cur_format = cur_surf_info.format;
return cur_surf_info;
}

static void set_screen_palette(const blit::Pen *colours, int num_cols) {
memcpy(palette, colours, num_cols * sizeof(blit::Pen));
Expand All @@ -60,11 +44,11 @@ static bool set_screen_mode_format(blit::ScreenMode new_mode, blit::SurfaceTempl

switch(new_mode) {
case blit::ScreenMode::lores:
new_surf_template.bounds = __fb_lores.bounds;
new_surf_template.bounds = blit::Size(System::width / 2, System::height / 2);
break;
case blit::ScreenMode::hires:
case blit::ScreenMode::hires_palette:
new_surf_template.bounds = __fb_hires.bounds;
new_surf_template.bounds = blit::Size(System::width, System::height);
break;
}

Expand All @@ -80,12 +64,26 @@ static bool set_screen_mode_format(blit::ScreenMode new_mode, blit::SurfaceTempl
return false;
}

_mode = new_mode;
cur_format = new_surf_template.format;
requested_mode = new_mode;
requested_format = new_surf_template.format;

return true;
}

blit::SurfaceInfo &set_screen_mode(blit::ScreenMode new_mode) {
blit::SurfaceTemplate temp{nullptr, {0, 0}, new_mode == blit::ScreenMode::hires_palette ? blit::PixelFormat::P : blit::PixelFormat::RGB};

// won't fail for the modes used here
set_screen_mode_format(new_mode, temp);

cur_surf_info.data = temp.data;
cur_surf_info.bounds = temp.bounds;
cur_surf_info.format = temp.format;
cur_surf_info.palette = temp.palette;

return cur_surf_info;
}

// blit timer callback
std::chrono::steady_clock::time_point start;
uint32_t now() {
Expand Down Expand Up @@ -315,6 +313,11 @@ void System::loop() {
{
blit::render(time_now);
last_render_time = time_now;

if(_mode != requested_mode || cur_format != requested_format) {
_mode = requested_mode;
cur_format = requested_format;
}
}

blit::tick(::now());
Expand All @@ -336,7 +339,7 @@ void System::update_texture(SDL_Texture *texture) {
auto stride = (is_lores ? width / 2 : width) * blit::pixel_format_stride[int(cur_format)];

if(cur_format == blit::PixelFormat::P) {
uint8_t col_fb[width * height * 3];
uint8_t col_fb[max_width * max_height * 3];

auto in = framebuffer, out = col_fb;
auto size = is_lores ? (width / 2) * (height / 2) : width * height;
Expand Down
7 changes: 5 additions & 2 deletions 32blit-sdl/System.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ class System {
static const Uint32 timer_event;
static const Uint32 loop_event;

static const int width = 320;
static const int height = 240;
static const int max_width = 320;
static const int max_height = 240;

static int width;
static int height;

System();
~System();
Expand Down
2 changes: 1 addition & 1 deletion 32blit/graphics/primitive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace blit {
return;
}

for (uint8_t y = cr.y; y < cr.y + cr.h; y++) {
for (int32_t y = cr.y; y < cr.y + cr.h; y++) {
pbf(&pen, this, o, cr.w);
o += bounds.w;
}
Expand Down

0 comments on commit fc23c1d

Please sign in to comment.