| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "video.h" |
| 20 | |
| 21 | #include "keyboard.h" |
| 22 | #include "opentyr.h" |
| 23 | #include "palette.h" |
| 24 | #include "video_scale.h" |
| 25 | |
| 26 | #include <assert.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | const char *const scaling_mode_names[ScalingMode_MAX] = { |
| 33 | "Center", |
| 34 | "Integer", |
| 35 | "Fit 8:5", |
| 36 | "Fit 4:3", |
| 37 | }; |
| 38 | |
| 39 | int fullscreen_display; |
| 40 | ScalingMode scaling_mode = SCALE_INTEGER; |
| 41 | static SDL_Rect last_output_rect = { 0, 0, vga_width, vga_height }; |
| 42 | |
| 43 | SDL_Surface *VGAScreen, *VGAScreenSeg; |
| 44 | SDL_Surface *VGAScreen2; |
| 45 | SDL_Surface *game_screen; |
| 46 | |
| 47 | SDL_Window *main_window = NULL; |
| 48 | static SDL_Renderer *main_window_renderer = NULL; |
| 49 | SDL_PixelFormat *main_window_tex_format = NULL; |
| 50 | static SDL_Texture *main_window_texture = NULL; |
| 51 | |
| 52 | static ScalerFunction scaler_function; |
| 53 | |
| 54 | static void init_renderer(void); |
| 55 | static void deinit_renderer(void); |
| 56 | static void init_texture(void); |
| 57 | static void deinit_texture(void); |
| 58 | |
| 59 | static int window_get_display_index(void); |
| 60 | static void window_center_in_display(int display_index); |
| 61 | static void calc_dst_render_rect(SDL_Surface *src_surface, SDL_Rect *dst_rect); |
| 62 | static void scale_and_flip(SDL_Surface *); |
| 63 | |
| 64 | void init_video(void) |
| 65 | { |
| 66 | if (SDL_WasInit(SDL_INIT_VIDEO)) |
| 67 | return; |
| 68 | |
| 69 | if (SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) |
| 70 | { |
| 71 | fprintf(stderr, "error: failed to initialize SDL video: %s\n", SDL_GetError()); |
| 72 | exit(1); |
| 73 | } |
| 74 | |
| 75 | // Create the software surfaces that the game renders to. These are all 320x200x8 regardless |
| 76 | // of the window size or monitor resolution. |
| 77 | VGAScreen = VGAScreenSeg = SDL_CreateRGBSurface(0, vga_width, vga_height, 8, 0, 0, 0, 0); |
| 78 | VGAScreen2 = SDL_CreateRGBSurface(0, vga_width, vga_height, 8, 0, 0, 0, 0); |
| 79 | game_screen = SDL_CreateRGBSurface(0, vga_width, vga_height, 8, 0, 0, 0, 0); |
| 80 | |
| 81 | // The game code writes to surface->pixels directly without locking, so make sure that we |
| 82 | // indeed created software surfaces that support this. |
| 83 | assert(!SDL_MUSTLOCK(VGAScreen)); |
| 84 | assert(!SDL_MUSTLOCK(VGAScreen2)); |
| 85 | assert(!SDL_MUSTLOCK(game_screen)); |
| 86 | |
| 87 | JE_clr256(VGAScreen); |
| 88 | |
| 89 | // Create the window with a temporary initial size, hidden until we set up the |
| 90 | // scaler and find the true window size |
| 91 | main_window = SDL_CreateWindow("OpenTyrian", |
| 92 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
| 93 | vga_width, vga_height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN); |
| 94 | |
| 95 | if (main_window == NULL) |
| 96 | { |
| 97 | fprintf(stderr, "error: failed to create window: %s\n", SDL_GetError()); |
| 98 | exit(EXIT_FAILURE); |
| 99 | } |
| 100 | |
| 101 | reinit_fullscreen(fullscreen_display); |
| 102 | init_renderer(); |
| 103 | init_texture(); |
| 104 | init_scaler(scaler); |
| 105 | |
| 106 | SDL_ShowWindow(main_window); |
| 107 | |
| 108 | SDL_SetRenderDrawColor(main_window_renderer, 0, 0, 0, 255); |
| 109 | SDL_RenderClear(main_window_renderer); |
| 110 | SDL_RenderPresent(main_window_renderer); |
| 111 | } |
| 112 | |
| 113 | void deinit_video(void) |
| 114 | { |
| 115 | deinit_texture(); |
| 116 | deinit_renderer(); |
| 117 | |
| 118 | SDL_DestroyWindow(main_window); |
| 119 | |
| 120 | SDL_FreeSurface(VGAScreenSeg); |
| 121 | SDL_FreeSurface(VGAScreen2); |
| 122 | SDL_FreeSurface(game_screen); |
| 123 | |
| 124 | SDL_QuitSubSystem(SDL_INIT_VIDEO); |
| 125 | } |
| 126 | |
| 127 | static void init_renderer(void) |
| 128 | { |
| 129 | main_window_renderer = SDL_CreateRenderer(main_window, -1, 0); |
| 130 | |
| 131 | if (main_window_renderer == NULL) |
| 132 | { |
| 133 | fprintf(stderr, "error: failed to create renderer: %s\n", SDL_GetError()); |
| 134 | exit(EXIT_FAILURE); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void deinit_renderer(void) |
| 139 | { |
| 140 | if (main_window_renderer != NULL) |
| 141 | { |
| 142 | SDL_DestroyRenderer(main_window_renderer); |
| 143 | main_window_renderer = NULL; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void init_texture(void) |
| 148 | { |
| 149 | assert(main_window_renderer != NULL); |
| 150 | |
| 151 | int bpp = 32; // TODOSDL2 |
| 152 | Uint32 format = bpp == 32 ? SDL_PIXELFORMAT_RGB888 : SDL_PIXELFORMAT_RGB565; |
| 153 | int scaler_w = scalers[scaler].width; |
| 154 | int scaler_h = scalers[scaler].height; |
| 155 | |
| 156 | main_window_tex_format = SDL_AllocFormat(format); |
| 157 | |
| 158 | main_window_texture = SDL_CreateTexture(main_window_renderer, format, SDL_TEXTUREACCESS_STREAMING, scaler_w, scaler_h); |
| 159 | |
| 160 | if (main_window_texture == NULL) |
| 161 | { |
| 162 | fprintf(stderr, "error: failed to create scaler texture %dx%dx%s: %s\n", scaler_w, scaler_h, SDL_GetPixelFormatName(format), SDL_GetError()); |
| 163 | exit(EXIT_FAILURE); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void deinit_texture(void) |
| 168 | { |
| 169 | if (main_window_texture != NULL) |
| 170 | { |
| 171 | SDL_DestroyTexture(main_window_texture); |
| 172 | main_window_texture = NULL; |
| 173 | } |
| 174 | |
| 175 | if (main_window_tex_format != NULL) |
| 176 | { |
| 177 | SDL_FreeFormat(main_window_tex_format); |
| 178 | main_window_tex_format = NULL; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static int window_get_display_index(void) |
| 183 | { |
| 184 | return SDL_GetWindowDisplayIndex(main_window); |
| 185 | } |
| 186 | |
| 187 | static void window_center_in_display(int display_index) |
| 188 | { |
| 189 | int win_w, win_h; |
| 190 | SDL_GetWindowSize(main_window, &win_w, &win_h); |
| 191 | |
| 192 | SDL_Rect bounds; |
| 193 | SDL_GetDisplayBounds(display_index, &bounds); |
| 194 | |
| 195 | SDL_SetWindowPosition(main_window, bounds.x + (bounds.w - win_w) / 2, bounds.y + (bounds.h - win_h) / 2); |
| 196 | } |
| 197 | |
| 198 | void reinit_fullscreen(int new_display) |
| 199 | { |
| 200 | fullscreen_display = new_display; |
| 201 | |
| 202 | if (fullscreen_display >= SDL_GetNumVideoDisplays()) |
| 203 | { |
| 204 | fullscreen_display = 0; |
| 205 | } |
| 206 | |
| 207 | SDL_SetWindowFullscreen(main_window, SDL_FALSE); |
| 208 | SDL_SetWindowSize(main_window, scalers[scaler].width, scalers[scaler].height); |
| 209 | |
| 210 | if (fullscreen_display == -1) |
| 211 | { |
| 212 | window_center_in_display(window_get_display_index()); |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | window_center_in_display(fullscreen_display); |
| 217 | |
| 218 | if (SDL_SetWindowFullscreen(main_window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0) |
| 219 | { |
| 220 | reinit_fullscreen(-1); |
| 221 | return; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | void video_on_win_resize(void) |
| 227 | { |
| 228 | int w, h; |
| 229 | int scaler_w, scaler_h; |
| 230 | |
| 231 | // Tell video to reinit if the window was manually resized by the user. |
| 232 | // Also enforce a minimum size on the window. |
| 233 | |
| 234 | SDL_GetWindowSize(main_window, &w, &h); |
| 235 | scaler_w = scalers[scaler].width; |
| 236 | scaler_h = scalers[scaler].height; |
| 237 | |
| 238 | if (w < scaler_w || h < scaler_h) |
| 239 | { |
| 240 | w = w < scaler_w ? scaler_w : w; |
| 241 | h = h < scaler_h ? scaler_h : h; |
| 242 | |
| 243 | SDL_SetWindowSize(main_window, w, h); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void toggle_fullscreen(void) |
| 248 | { |
| 249 | if (fullscreen_display != -1) |
| 250 | reinit_fullscreen(-1); |
| 251 | else |
| 252 | reinit_fullscreen(SDL_GetWindowDisplayIndex(main_window)); |
| 253 | } |
| 254 | |
| 255 | bool init_scaler(unsigned int new_scaler) |
| 256 | { |
| 257 | int w = scalers[new_scaler].width, |
| 258 | h = scalers[new_scaler].height; |
| 259 | int bpp = main_window_tex_format->BitsPerPixel; // TODOSDL2 |
| 260 | |
| 261 | scaler = new_scaler; |
| 262 | |
| 263 | deinit_texture(); |
| 264 | init_texture(); |
| 265 | |
| 266 | if (fullscreen_display == -1) |
| 267 | { |
| 268 | // Changing scalers, when not in fullscreen mode, forces the window |
| 269 | // to resize to exactly match the scaler's output dimensions. |
| 270 | SDL_SetWindowSize(main_window, w, h); |
| 271 | window_center_in_display(window_get_display_index()); |
| 272 | } |
| 273 | |
| 274 | switch (bpp) |
| 275 | { |
| 276 | case 32: |
| 277 | scaler_function = scalers[scaler].scaler32; |
| 278 | break; |
| 279 | case 16: |
| 280 | scaler_function = scalers[scaler].scaler16; |
| 281 | break; |
| 282 | default: |
| 283 | scaler_function = NULL; |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | if (scaler_function == NULL) |
| 288 | { |
| 289 | assert(false); |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | bool set_scaling_mode_by_name(const char *name) |
| 297 | { |
| 298 | for (int i = 0; i < ScalingMode_MAX; ++i) |
| 299 | { |
| 300 | if (strcmp(name, scaling_mode_names[i]) == 0) |
| 301 | { |
| 302 | scaling_mode = i; |
| 303 | return true; |
| 304 | } |
| 305 | } |
| 306 | return false; |
| 307 | } |
| 308 | |
| 309 | void JE_clr256(SDL_Surface *screen) |
| 310 | { |
| 311 | SDL_FillRect(screen, NULL, 0); |
| 312 | } |
| 313 | |
| 314 | void JE_showVGA(void) |
| 315 | { |
| 316 | scale_and_flip(VGAScreen); |
| 317 | } |
| 318 | |
| 319 | static void calc_dst_render_rect(SDL_Surface *const src_surface, SDL_Rect *const dst_rect) |
| 320 | { |
| 321 | // Decides how the logical output texture (after software scaling applied) will fit |
| 322 | // in the window. |
| 323 | |
| 324 | int win_w, win_h; |
| 325 | SDL_GetWindowSize(main_window, &win_w, &win_h); |
| 326 | |
| 327 | int maxh_width, maxw_height; |
| 328 | |
| 329 | switch (scaling_mode) |
| 330 | { |
| 331 | case SCALE_CENTER: |
| 332 | SDL_QueryTexture(main_window_texture, NULL, NULL, &dst_rect->w, &dst_rect->h); |
| 333 | break; |
| 334 | case SCALE_INTEGER: |
| 335 | dst_rect->w = src_surface->w; |
| 336 | dst_rect->h = src_surface->h; |
| 337 | while (dst_rect->w + src_surface->w <= win_w && dst_rect->h + src_surface->h <= win_h) |
| 338 | { |
| 339 | dst_rect->w += src_surface->w; |
| 340 | dst_rect->h += src_surface->h; |
| 341 | } |
| 342 | break; |
| 343 | case SCALE_ASPECT_8_5: |
| 344 | maxh_width = win_h * (8.f / 5.f); |
| 345 | maxw_height = win_w * (5.f / 8.f); |
| 346 | |
| 347 | if (maxh_width > win_w) |
| 348 | { |
| 349 | dst_rect->w = win_w; |
| 350 | dst_rect->h = maxw_height; |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | dst_rect->w = maxh_width; |
| 355 | dst_rect->h = win_h; |
| 356 | } |
| 357 | break; |
| 358 | case SCALE_ASPECT_4_3: |
| 359 | maxh_width = win_h * (4.f / 3.f); |
| 360 | maxw_height = win_w * (3.f / 4.f); |
| 361 | |
| 362 | if (maxh_width > win_w) |
| 363 | { |
| 364 | dst_rect->w = win_w; |
| 365 | dst_rect->h = maxw_height; |
| 366 | } |
| 367 | else |
| 368 | { |
| 369 | dst_rect->w = maxh_width; |
| 370 | dst_rect->h = win_h; |
| 371 | } |
| 372 | break; |
| 373 | case ScalingMode_MAX: |
| 374 | assert(false); |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | dst_rect->x = (win_w - dst_rect->w) / 2; |
| 379 | dst_rect->y = (win_h - dst_rect->h) / 2; |
| 380 | } |
| 381 | |
| 382 | static void scale_and_flip(SDL_Surface *src_surface) |
| 383 | { |
| 384 | assert(src_surface->format->BitsPerPixel == 8); |
| 385 | |
| 386 | // Do software scaling |
| 387 | assert(scaler_function != NULL); |
| 388 | scaler_function(src_surface, main_window_texture); |
| 389 | |
| 390 | SDL_Rect dst_rect; |
| 391 | calc_dst_render_rect(src_surface, &dst_rect); |
| 392 | |
| 393 | // Clear the window and blit the output texture to it |
| 394 | SDL_SetRenderDrawColor(main_window_renderer, 0, 0, 0, 255); |
| 395 | SDL_RenderClear(main_window_renderer); |
| 396 | SDL_RenderCopy(main_window_renderer, main_window_texture, NULL, &dst_rect); |
| 397 | SDL_RenderPresent(main_window_renderer); |
| 398 | |
| 399 | // Save output rect to be used by mouse functions |
| 400 | last_output_rect = dst_rect; |
| 401 | } |
| 402 | |
| 403 | /** Maps a specified point in game screen coordinates to window coordinates. */ |
| 404 | void mapScreenPointToWindow(Sint32 *const inout_x, Sint32 *const inout_y) |
| 405 | { |
| 406 | *inout_x = (2 * *inout_x + 1) * last_output_rect.w / (2 * VGAScreen->w) + last_output_rect.x; |
| 407 | *inout_y = (2 * *inout_y + 1) * last_output_rect.h / (2 * VGAScreen->h) + last_output_rect.y; |
| 408 | } |
| 409 | |
| 410 | /** Maps a specified point in window coordinates to game screen coordinates. */ |
| 411 | void mapWindowPointToScreen(Sint32 *const inout_x, Sint32 *const inout_y) |
| 412 | { |
| 413 | *inout_x = (2 * (*inout_x - last_output_rect.x) + 1) * VGAScreen->w / (2 * last_output_rect.w); |
| 414 | *inout_y = (2 * (*inout_y - last_output_rect.y) + 1) * VGAScreen->h / (2 * last_output_rect.h); |
| 415 | } |
| 416 | |
| 417 | /** Scales a distance in window coordinates to game screen coordinates. */ |
| 418 | void scaleWindowDistanceToScreen(Sint32 *const inout_x, Sint32 *const inout_y) |
| 419 | { |
| 420 | *inout_x = (2 * *inout_x + 1) * VGAScreen->w / (2 * last_output_rect.w); |
| 421 | *inout_y = (2 * *inout_y + 1) * VGAScreen->h / (2 * last_output_rect.h); |
| 422 | } |