src/video/sdl_v.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9911 0b8b245a2391
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    11 #include "../macros.h"
    11 #include "../macros.h"
    12 #include "../sdl.h"
    12 #include "../sdl.h"
    13 #include "../window.h"
    13 #include "../window.h"
    14 #include "../network/network.h"
    14 #include "../network/network.h"
    15 #include "../variables.h"
    15 #include "../variables.h"
       
    16 #include "../blitter/factory.hpp"
    16 #include "sdl_v.h"
    17 #include "sdl_v.h"
    17 #include <SDL.h>
    18 #include <SDL.h>
    18 
    19 
    19 static SDL_Surface *_sdl_screen;
    20 static SDL_Surface *_sdl_screen;
    20 static bool _all_modes;
    21 static bool _all_modes;
    34 	_num_dirty_rects++;
    35 	_num_dirty_rects++;
    35 }
    36 }
    36 
    37 
    37 static void UpdatePalette(uint start, uint count)
    38 static void UpdatePalette(uint start, uint count)
    38 {
    39 {
       
    40 	/* We can only update the palette in 8bpp for now */
       
    41 	/* TODO -- We need support for other bpps too! */
       
    42 	if (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() != 8) return;
       
    43 
    39 	SDL_Color pal[256];
    44 	SDL_Color pal[256];
    40 	uint i;
    45 	uint i;
    41 
    46 
    42 	for (i = 0; i != count; i++) {
    47 	for (i = 0; i != count; i++) {
    43 		pal[i].r = _cur_palette[start + i].r;
    48 		pal[i].r = _cur_palette[start + i].r;
   170 static bool CreateMainSurface(int w, int h)
   175 static bool CreateMainSurface(int w, int h)
   171 {
   176 {
   172 	extern const char _openttd_revision[];
   177 	extern const char _openttd_revision[];
   173 	SDL_Surface *newscreen, *icon;
   178 	SDL_Surface *newscreen, *icon;
   174 	char caption[50];
   179 	char caption[50];
       
   180 	int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
   175 
   181 
   176 	GetAvailableVideoMode(&w, &h);
   182 	GetAvailableVideoMode(&w, &h);
   177 
   183 
   178 	DEBUG(driver, 1, "SDL: using mode %dx%d", w, h);
   184 	DEBUG(driver, 1, "SDL: using mode %dx%dx%d", w, h, bpp);
       
   185 
       
   186 	if (bpp == 0) error("Can't use a blitter that blits 0 bpp for normal visuals");
   179 
   187 
   180 	/* Give the application an icon */
   188 	/* Give the application an icon */
   181 	icon = SDL_CALL SDL_LoadBMP(ICON_DIR PATHSEP "openttd.32.bmp");
   189 	icon = SDL_CALL SDL_LoadBMP(ICON_DIR PATHSEP "openttd.32.bmp");
   182 	if (icon != NULL) {
   190 	if (icon != NULL) {
   183 		/* Get the colourkey, which will be magenta */
   191 		/* Get the colourkey, which will be magenta */
   187 		SDL_CALL SDL_WM_SetIcon(icon, NULL);
   195 		SDL_CALL SDL_WM_SetIcon(icon, NULL);
   188 		SDL_CALL SDL_FreeSurface(icon);
   196 		SDL_CALL SDL_FreeSurface(icon);
   189 	}
   197 	}
   190 
   198 
   191 	// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK
   199 	// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK
   192 	newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
   200 	newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
   193 	if (newscreen == NULL)
   201 	if (newscreen == NULL)
   194 		return false;
   202 		return false;
   195 
   203 
   196 	_screen.width = newscreen->w;
   204 	_screen.width = newscreen->w;
   197 	_screen.height = newscreen->h;
   205 	_screen.height = newscreen->h;
   198 	_screen.pitch = newscreen->pitch / sizeof(Pixel);
   206 	_screen.pitch = newscreen->pitch / (bpp / 8);
   199 
       
   200 	_sdl_screen = newscreen;
   207 	_sdl_screen = newscreen;
   201 	InitPalette();
   208 	InitPalette();
   202 
   209 
   203 	snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
   210 	snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
   204 	SDL_CALL SDL_WM_SetCaption(caption, caption);
   211 	SDL_CALL SDL_WM_SetCaption(caption, caption);
   467 				(keys[SDLK_UP]    ? 2 : 0) |
   474 				(keys[SDLK_UP]    ? 2 : 0) |
   468 				(keys[SDLK_RIGHT] ? 4 : 0) |
   475 				(keys[SDLK_RIGHT] ? 4 : 0) |
   469 				(keys[SDLK_DOWN]  ? 8 : 0);
   476 				(keys[SDLK_DOWN]  ? 8 : 0);
   470 			GameLoop();
   477 			GameLoop();
   471 
   478 
   472 			_screen.dst_ptr = (Pixel*)_sdl_screen->pixels;
   479 			_screen.dst_ptr = _sdl_screen->pixels;
   473 			UpdateWindows();
   480 			UpdateWindows();
   474 			if (++pal_tick > 4) {
   481 			if (++pal_tick > 4) {
   475 				CheckPaletteAnim();
   482 				CheckPaletteAnim();
   476 				pal_tick = 1;
   483 				pal_tick = 1;
   477 			}
   484 			}
   478 			DrawSurfaceToScreen();
   485 			DrawSurfaceToScreen();
   479 		} else {
   486 		} else {
   480 			SDL_CALL SDL_Delay(1);
   487 			SDL_CALL SDL_Delay(1);
   481 			_screen.dst_ptr = (Pixel*)_sdl_screen->pixels;
   488 			_screen.dst_ptr = _sdl_screen->pixels;
   482 			DrawTextMessage();
   489 			DrawTextMessage();
   483 			DrawMouseCursor();
   490 			DrawMouseCursor();
   484 			DrawSurfaceToScreen();
   491 			DrawSurfaceToScreen();
   485 		}
   492 		}
   486 	}
   493 	}