port sdl2 x4 patch

This commit is contained in:
Tim Felgentreff 2015-12-20 18:32:16 +01:00
parent 7a1d94e811
commit 59ac576e25
8 changed files with 126 additions and 43 deletions

View file

@ -825,7 +825,7 @@ endif()
if(APPLE)
add_definitions(-DUSE_MAC)
set(SDL_LIBRARY ${SDL_LIBRARY} SDLmain)
set(SDL2_LIBRARY ${SDL2_LIBRARY} SDL2main)
endif()
if(BSD)
@ -1090,7 +1090,7 @@ if(WIN32 AND MINGW AND ENABLE_STATIC)
endif()
if(APPLE)
set_target_properties(stratagus PROPERTIES LINK_FLAGS "${LINK_FLAGS} -lSDLmain")
set_target_properties(stratagus PROPERTIES LINK_FLAGS "${LINK_FLAGS} -framework libpng -lSDL2main")
endif()
########### next target ###############

View file

@ -89,6 +89,7 @@ namespace gcn
*
* @param target the target to draw to.
*/
//virtual void setTarget(SDL_Renderer* renderer);
virtual void setTarget(SDL_Surface* target);
//virtual void setTarget(SDL_Renderer* renderer);

View file

@ -698,8 +698,8 @@ void CMap::InitFogOfWar()
// Generate Alpha Fog surface.
//
if (FogGraphic->Surface->format->BytesPerPixel == 1) {
s = FogGraphic->Surface;
SDL_SetSurfaceAlphaMod(FogGraphic->Surface, FogOfWarOpacity);
s = SDL_ConvertSurfaceFormat(FogGraphic->Surface, SDL_PIXELFORMAT_RGB888, 0);
SDL_SetSurfaceAlphaMod(s, FogOfWarOpacity);
} else {
// Copy the top row to a new surface
SDL_PixelFormat *f = FogGraphic->Surface->format;

View file

@ -1207,7 +1207,7 @@ void GraphicPlayerPixels(CPlayer &player, const CGraphic &sprite)
{
Assert(PlayerColorIndexCount);
SDL_LockSurface(sprite.Surface);
//SDL_LockSurface(sprite.Surface);
std::vector<SDL_Color> sdlColors(player.UnitColors.Colors.begin(), player.UnitColors.Colors.end());
SDL_SetPaletteColors(sprite.Surface->format->palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
if (sprite.SurfaceFlip) {
@ -1215,7 +1215,7 @@ void GraphicPlayerPixels(CPlayer &player, const CGraphic &sprite)
SDL_SetPaletteColors(sprite.SurfaceFlip->format->palette, &sdlColors[0], PlayerColorIndexStart, PlayerColorIndexCount);
//SDL_UnlockSurface(sprite.SurfaceFlip);
}
SDL_UnlockSurface(sprite.Surface);
//SDL_UnlockSurface(sprite.Surface);
}
/**

View file

@ -60,7 +60,7 @@ void CColor::Parse(lua_State *l, const int offset)
this->R = r;
this->G = g;
this->B = b;
this->A = 0;
this->A = 255;
}
//@}

View file

@ -116,7 +116,38 @@ void CGraphic::DrawSubClip(int gx, int gy, int w, int h, int x, int y) const
int oldx = x;
int oldy = y;
CLIP_RECTANGLE(x, y, w, h);
DrawSub(gx + x - oldx, gy + y - oldy, w, h, x, y);
gx += x - oldx;
gy += y - oldy;
#if 1
if (File=="graphics/human/units/elven_archer.png" && Surface->format->BytesPerPixel == 1)
{
//FILE *file = fopen("archer-test.data", "w+");
FILE *file = fopen("archer-palete-draw.data", "w+");
if (file) {
//fwrite(s->pixels, 1, s->pitch*s->h, file);
//fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
for (int i = 0; i < Surface->format->palette->ncolors; i++)
fwrite(&Surface->format->palette->colors[i], 1, 4, file);
fclose(file);
}
}
#endif
//DrawSub(gx + x - oldx, gy + y - oldy, w, h, x, y);
SDL_Rect srect = {Sint16(gx), Sint16(gy), Uint16(w), Uint16(h)};
SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0};
//SDL_LockSurface(TheScreen);
int ret;
ret = SDL_BlitSurface(Surface, &srect, TheScreen, &drect);
//SDL_UnlockSurface(TheScreen);
#if 1
if (File=="graphics/human/units/elven_archer.png" && Surface->format->BytesPerPixel == 1)
{
fprintf(stderr, "drawing non-flipped archer, srect %d, %d, %dx%d on %d, %d\n", srect.x, srect.y, w, h, drect.x, drect.y);
fprintf(stderr, "format %x\n", Surface->format->format);
if (ret) fprintf(stderr, "%s\n", SDL_GetError());
}
#endif
}
/**
@ -142,7 +173,7 @@ void CGraphic::DrawSubTrans(int gx, int gy, int w, int h, int x, int y,
} else
#endif
{
Uint8 oldalpha;
Uint8 oldalpha = 0xff;
SDL_GetSurfaceAlphaMod(Surface, &oldalpha);
SDL_SetSurfaceAlphaMod(Surface, alpha);
DrawSub(gx, gy, w, h, x, y);
@ -359,14 +390,38 @@ void CGraphic::DrawFrameClipX(unsigned frame, int x, int y) const
const int oldx = x;
const int oldy = y;
CLIP_RECTANGLE(x, y, srect.w, srect.h);
#if 1
if (File=="graphics/human/units/elven_archer.png" && SurfaceFlip->format->BytesPerPixel == 1)
{
//FILE *file = fopen("archer-test.data", "w+");
FILE *file = fopen("archer-palete-flip-draw.data", "w+");
if (file) {
//fwrite(s->pixels, 1, s->pitch*s->h, file);
//fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
for (int i = 0; i < SurfaceFlip->format->palette->ncolors; i++)
fwrite(&SurfaceFlip->format->palette->colors[i], 1, 4, file);
fclose(file);
}
}
#endif
srect.x += x - oldx;
srect.y += y - oldy;
SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0};
int ret;
//SDL_LockSurface(TheScreen);
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
//SDL_SetSurfaceAlphaMod(SurfaceFlip, 0xFF);
ret = SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
//SDL_UnlockSurface(TheScreen);
#if 1
if (File=="graphics/human/units/elven_archer.png" && SurfaceFlip->format->BytesPerPixel == 1)
{
fprintf(stderr, "drawing flipped archer, srect %d, %d, %dx%d on %d, %d\n", srect.x+x-oldx, srect.y, srect.w, srect.h, drect.x, drect.y);
fprintf(stderr, "format %x\n", SurfaceFlip->format->format);
if (ret) fprintf(stderr, "flip: %s\n", SDL_GetError());
}
#endif
}
}
@ -383,14 +438,14 @@ void CGraphic::DrawFrameTransX(unsigned frame, int x, int y, int alpha) const
{
SDL_Rect srect = {frameFlip_map[frame].x, frameFlip_map[frame].y, Uint16(Width), Uint16(Height)};
SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0};
Uint8 oldalpha;
SDL_GetSurfaceAlphaMod(Surface, &oldalpha);
Uint8 oldalpha = 0xff;
SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha);
SDL_SetSurfaceAlphaMod(Surface, alpha);
SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha);
//SDL_LockSurface(TheScreen);
SDL_BlitSurface(SurfaceFlip, &srect, TheScreen, &drect);
//SDL_UnlockSurface(TheScreen);
SDL_SetSurfaceAlphaMod(Surface, oldalpha);
SDL_SetSurfaceAlphaMod(SurfaceFlip, oldalpha);
}
}
@ -407,14 +462,14 @@ void CGraphic::DrawFrameClipTransX(unsigned frame, int x, int y, int alpha) cons
{
SDL_Rect srect = {frameFlip_map[frame].x, frameFlip_map[frame].y, Uint16(Width), Uint16(Height)};
const int oldx = x;
const int oldy = y;
int oldx = x;
int oldy = y;
CLIP_RECTANGLE(x, y, srect.w, srect.h);
srect.x += x - oldx;
srect.y += y - oldy;
SDL_Rect drect = {Sint16(x), Sint16(y), 0, 0};
Uint8 oldalpha;
Uint8 oldalpha = 0xff;
SDL_GetSurfaceAlphaMod(SurfaceFlip, &oldalpha);
SDL_SetSurfaceAlphaMod(SurfaceFlip, alpha);
@ -740,6 +795,20 @@ void CGraphic::Load(bool grayscale)
Assert(Width <= GraphicWidth && Height <= GraphicHeight);
#if 1
if (File=="graphics/human/units/elven_archer.png" && Surface->format->BytesPerPixel == 1)
{
//FILE *file = fopen("archer-test.data", "w+");
FILE *file = fopen("archer-palete-orig.data", "w+");
if (file) {
//fwrite(s->pixels, 1, s->pitch*s->h, file);
//fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
for (int i = 0; i < Surface->format->palette->ncolors; i++)
fwrite(&Surface->format->palette->colors[i], 1, 3, file);
fclose(file);
}
}
#endif
if ((GraphicWidth / Width) * Width != GraphicWidth ||
(GraphicHeight / Height) * Height != GraphicHeight) {
fprintf(stderr, "Invalid graphic (width, height) %s\n", File.c_str());
@ -910,13 +979,15 @@ void CGraphic::Flip()
if (!SDL_GetColorKey(Surface, &ckey)) {
SDL_SetColorKey(SurfaceFlip, SDL_TRUE, ckey);
}
//SDL_SetSurfaceAlphaMod(SurfaceFlip, 0xFF);
SDL_SetSurfaceBlendMode(SurfaceFlip, SDL_BLENDMODE_NONE);
if (SurfaceFlip->format->BytesPerPixel == 1) {
//SDL_SetPaletteColors(SurfaceFlip->format->palette, Surface->format->palette->colors, 0, Surface->format->palette->ncolors);
VideoPaletteListAdd(SurfaceFlip);
}
SDL_LockSurface(Surface);
SDL_LockSurface(s);
fprintf(stderr, "flipping sprite %s (%dbpp)\n", File.c_str(), 8*s->format->BytesPerPixel);
//fprintf(stderr, "flipping sprite %s (%dbpp)\n", File.c_str(), 8*s->format->BytesPerPixel);
switch (s->format->BytesPerPixel) {
case 1:
for (int i = 0; i < s->h; ++i) {
@ -997,14 +1068,30 @@ void CGraphic::Flip()
}
break;
}
#if 0
if (File=="graphics/human/units/elven_archer.png" && s->format->BytesPerPixel == 1)
#if 1
if (File=="graphics/human/units/elven_archer.png" && Surface->format->BytesPerPixel == 1)
{
//FILE *file = fopen("archer-test.data", "w+");
FILE *file = fopen("archer-palete.data", "w+");
if (file) {
//fwrite(s->pixels, 1, s->pitch*s->h, file);
fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
//fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
for (int i = 0; i < Surface->format->palette->ncolors; i++)
fwrite(&Surface->format->palette->colors[i], 1, 3, file);
fclose(file);
}
}
#endif
#if 1
if (File=="graphics/human/units/elven_archer.png" && s->format->BytesPerPixel == 1)
{
//FILE *file = fopen("archer-test.data", "w+");
FILE *file = fopen("archer-palete-flip.data", "w+");
if (file) {
//fwrite(s->pixels, 1, s->pitch*s->h, file);
//fwrite(s->format->palette->colors, 1, 3 * s->format->palette->ncolors, file);
for (int i = 0; i < s->format->palette->ncolors; i++)
fwrite(&s->format->palette->colors[i], 1, 3, file);
fclose(file);
}
}
@ -1017,8 +1104,7 @@ void CGraphic::Flip()
frameFlip_map = new frame_pos_t[NumFrames];
for (int frame = 0; frame < NumFrames; ++frame) {
frameFlip_map[frame].x = (SurfaceFlip->w - (frame % (SurfaceFlip->w /
Width)) * Width) - Width;
frameFlip_map[frame].x = ((NumFrames - frame - 1) % (SurfaceFlip->w / Width)) * Width;
frameFlip_map[frame].y = (frame / (SurfaceFlip->w / Width)) * Height;
}
}
@ -1053,10 +1139,6 @@ void CGraphic::UseDisplayFormat()
VideoPaletteListRemove(s);
SDL_FreeSurface(s);
}
VideoPaletteListRemove(Surface);
if (SurfaceFlip) {
VideoPaletteListRemove(SurfaceFlip);
}
#endif
}

View file

@ -228,14 +228,6 @@ int LoadGraphicPNG(CGraphic *g)
return -1;
}
if (ckey != -1) {
if (color_type != PNG_COLOR_TYPE_PALETTE) {
/* FIXME: Should these be truncated or shifted down? */
ckey = SDL_MapRGB(surface->format, (Uint8)transv->red, (Uint8)transv->green, (Uint8)transv->blue);
}
SDL_SetColorKey(surface, SDL_TRUE, ckey);
}
/* Create the array of pointers to image data */
std::vector<png_bytep> row_pointers;
row_pointers.resize(height);
@ -277,6 +269,14 @@ int LoadGraphicPNG(CGraphic *g)
}
}
if (ckey != -1) {
if (color_type != PNG_COLOR_TYPE_PALETTE) {
/* FIXME: Should these be truncated or shifted down? */
ckey = SDL_MapRGB(surface->format, (Uint8)transv->red, (Uint8)transv->green, (Uint8)transv->blue);
}
SDL_SetColorKey(surface, SDL_TRUE, ckey);
}
g->Surface = surface;
g->GraphicWidth = surface->w;
g->GraphicHeight = surface->h;

View file

@ -826,26 +826,26 @@ static void SdlDoEvent(const EventCallback &callbacks, SDL_Event &event)
{
static bool InMainWindow = true;
if (InMainWindow && (event.window.event & SDL_WINDOWEVENT_LEAVE)) {
if (InMainWindow && (event.window.event == SDL_WINDOWEVENT_LEAVE)) {
InputMouseExit(callbacks, SDL_GetTicks());
}
InMainWindow = !!(event.window.event & SDL_WINDOWEVENT_ENTER);
InMainWindow = (event.window.event == SDL_WINDOWEVENT_ENTER);
}
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_FOCUS_LOST:
{
if (!IsNetworkGame() && Preference.PauseOnLeave /*SDL_GetAppState() & SDL_APPACTIVE*/) {
if (!IsNetworkGame() && Preference.PauseOnLeave /*(SDL_GetWindowFlags(TheWindow) & SDL_WINDOW_INPUT_FOCUS)*/) {
static bool DoTogglePause = false;
if (IsSDLWindowVisible && (event.window.event & SDL_WINDOWEVENT_FOCUS_LOST)) {
if (IsSDLWindowVisible && (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST)) {
IsSDLWindowVisible = false;
if (!GamePaused) {
DoTogglePause = true;
UiTogglePause();
}
} else if (!IsSDLWindowVisible && (event.window.event & SDL_WINDOWEVENT_FOCUS_GAINED)) {
} else if (!IsSDLWindowVisible && (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)) {
IsSDLWindowVisible = true;
if (GamePaused && DoTogglePause) {
DoTogglePause = false;
@ -1072,7 +1072,7 @@ int Str2SdlKey(const char *str)
*/
bool SdlGetGrabMouse()
{
return SDL_GetRelativeMouseMode();
return SDL_GetWindowGrab(TheWindow);
}
/**
@ -1085,9 +1085,9 @@ void ToggleGrabMouse(int mode)
bool grabbed = SdlGetGrabMouse();
if (mode <= 0 && grabbed) {
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_SetWindowGrab(TheWindow, SDL_FALSE);
} else if (mode >= 0 && !grabbed) {
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_SetWindowGrab(TheWindow, SDL_TRUE);
}
}