diff --git a/32blit-sdl/Main.cpp b/32blit-sdl/Main.cpp index 07618800d..4eb6a2c5b 100644 --- a/32blit-sdl/Main.cpp +++ b/32blit-sdl/Main.cpp @@ -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; @@ -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) { @@ -202,6 +210,7 @@ int main(int argc, char *argv[]) { std::cout << " --connect -- 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 -- 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; @@ -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) { @@ -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); diff --git a/32blit-sdl/System.cpp b/32blit-sdl/System.cpp index 996fc1b70..7b9fcc1f2 100644 --- a/32blit-sdl/System.cpp +++ b/32blit-sdl/System.cpp @@ -15,14 +15,13 @@ 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; @@ -30,26 +29,11 @@ void blit_debug(const char *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)); @@ -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; } @@ -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() { @@ -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()); @@ -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; diff --git a/32blit-sdl/System.hpp b/32blit-sdl/System.hpp index 4565887bc..a8149c9c3 100644 --- a/32blit-sdl/System.hpp +++ b/32blit-sdl/System.hpp @@ -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(); diff --git a/32blit/graphics/primitive.cpp b/32blit/graphics/primitive.cpp index 4ef9453e6..17baf833b 100644 --- a/32blit/graphics/primitive.cpp +++ b/32blit/graphics/primitive.cpp @@ -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; }