📄 sdl_ph_image.c
字号:
/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2006 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Sam Lantinga slouken@libsdl.org*/#include "SDL_config.h"#include <Ph.h>#include <photon/Pg.h>#include "SDL_endian.h"#include "SDL_video.h"#include "../SDL_pixels_c.h"#include "SDL_ph_video.h"#include "SDL_ph_image_c.h"#include "SDL_ph_modes_c.h"#include "SDL_ph_gl.h"int ph_SetupImage(_THIS, SDL_Surface *screen){ PgColor_t* palette=NULL; int type=0; int bpp; bpp=screen->format->BitsPerPixel; /* Determine image type */ switch(bpp) { case 8:{ type = Pg_IMAGE_PALETTE_BYTE; } break; case 15:{ type = Pg_IMAGE_DIRECT_555; } break; case 16:{ type = Pg_IMAGE_DIRECT_565; } break; case 24:{ type = Pg_IMAGE_DIRECT_888; } break; case 32:{ type = Pg_IMAGE_DIRECT_8888; } break; default:{ SDL_SetError("ph_SetupImage(): unsupported bpp=%d !\n", bpp); return -1; } break; } /* palette emulation code */ if ((bpp==8) && (desktoppal==SDLPH_PAL_EMULATE)) { /* creating image palette */ palette=SDL_malloc(_Pg_MAX_PALETTE*sizeof(PgColor_t)); if (palette==NULL) { SDL_SetError("ph_SetupImage(): can't allocate memory for palette !\n"); return -1; } PgGetPalette(palette); /* using shared memory for speed (set last param to 1) */ if ((SDL_Image = PhCreateImage(NULL, screen->w, screen->h, type, palette, _Pg_MAX_PALETTE, 1)) == NULL) { SDL_SetError("ph_SetupImage(): PhCreateImage() failed for bpp=8 !\n"); SDL_free(palette); return -1; } } else { /* using shared memory for speed (set last param to 1) */ if ((SDL_Image = PhCreateImage(NULL, screen->w, screen->h, type, NULL, 0, 1)) == NULL) { SDL_SetError("ph_SetupImage(): PhCreateImage() failed for bpp=%d !\n", bpp); return -1; } } screen->pixels = SDL_Image->image; screen->pitch = SDL_Image->bpl; this->UpdateRects = ph_NormalUpdate; return 0;}int ph_SetupOCImage(_THIS, SDL_Surface *screen){ int type = 0; int bpp; OCImage.flags = screen->flags; bpp=screen->format->BitsPerPixel; /* Determine image type */ switch(bpp) { case 8: { type = Pg_IMAGE_PALETTE_BYTE; } break; case 15:{ type = Pg_IMAGE_DIRECT_555; } break; case 16:{ type = Pg_IMAGE_DIRECT_565; } break; case 24:{ type = Pg_IMAGE_DIRECT_888; } break; case 32:{ type = Pg_IMAGE_DIRECT_8888; } break; default:{ SDL_SetError("ph_SetupOCImage(): unsupported bpp=%d !\n", bpp); return -1; } break; } /* Currently offscreen contexts with the same bit depth as display bpp only can be created */ OCImage.offscreen_context = PdCreateOffscreenContext(0, screen->w, screen->h, Pg_OSC_MEM_PAGE_ALIGN); if (OCImage.offscreen_context == NULL) { SDL_SetError("ph_SetupOCImage(): PdCreateOffscreenContext() function failed !\n"); return -1; } screen->pitch = OCImage.offscreen_context->pitch; OCImage.dc_ptr = (unsigned char *)PdGetOffscreenContextPtr(OCImage.offscreen_context); if (OCImage.dc_ptr == NULL) { SDL_SetError("ph_SetupOCImage(): PdGetOffscreenContextPtr function failed !\n"); PhDCRelease(OCImage.offscreen_context); return -1; } OCImage.FrameData0 = OCImage.dc_ptr; OCImage.CurrentFrameData = OCImage.FrameData0; OCImage.current = 0; PhDCSetCurrent(OCImage.offscreen_context); screen->pixels = OCImage.CurrentFrameData; this->UpdateRects = ph_OCUpdate; return 0;}int ph_SetupFullScreenImage(_THIS, SDL_Surface* screen){ OCImage.flags = screen->flags; /* Begin direct and fullscreen mode */ if (!ph_EnterFullScreen(this, screen, PH_ENTER_DIRECTMODE)) { return -1; } /* store palette for fullscreen */ if ((screen->format->BitsPerPixel==8) && (desktopbpp!=8)) { PgGetPalette(savedpal); PgGetPalette(syspalph); } OCImage.offscreen_context = PdCreateOffscreenContext(0, 0, 0, Pg_OSC_MAIN_DISPLAY | Pg_OSC_MEM_PAGE_ALIGN | Pg_OSC_CRTC_SAFE); if (OCImage.offscreen_context == NULL) { SDL_SetError("ph_SetupFullScreenImage(): PdCreateOffscreenContext() function failed !\n"); return -1; } if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { OCImage.offscreen_backcontext = PdDupOffscreenContext(OCImage.offscreen_context, Pg_OSC_MEM_PAGE_ALIGN | Pg_OSC_CRTC_SAFE); if (OCImage.offscreen_backcontext == NULL) { SDL_SetError("ph_SetupFullScreenImage(): PdCreateOffscreenContext(back) function failed !\n"); return -1; } } OCImage.FrameData0 = (unsigned char *)PdGetOffscreenContextPtr(OCImage.offscreen_context); if (OCImage.FrameData0 == NULL) { SDL_SetError("ph_SetupFullScreenImage(): PdGetOffscreenContextPtr() function failed !\n"); ph_DestroyImage(this, screen); return -1; } if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { OCImage.FrameData1 = (unsigned char *)PdGetOffscreenContextPtr(OCImage.offscreen_backcontext); if (OCImage.FrameData1 == NULL) { SDL_SetError("ph_SetupFullScreenImage(back): PdGetOffscreenContextPtr() function failed !\n"); ph_DestroyImage(this, screen); return -1; } } /* wait for the hardware */ PgFlush(); PgWaitHWIdle(); if ((screen->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF) { OCImage.current = 0; PhDCSetCurrent(OCImage.offscreen_context); screen->pitch = OCImage.offscreen_context->pitch; screen->pixels = OCImage.FrameData0; /* emulate 640x400 videomode */ if (videomode_emulatemode==1) { int i; for (i=0; i<40; i++) { SDL_memset(screen->pixels+screen->pitch*i, 0x00, screen->pitch); } for (i=440; i<480; i++) { SDL_memset(screen->pixels+screen->pitch*i, 0x00, screen->pitch); } screen->pixels+=screen->pitch*40; } PgSwapDisplay(OCImage.offscreen_backcontext, 0); } else { OCImage.current = 0; PhDCSetCurrent(OCImage.offscreen_context); screen->pitch = OCImage.offscreen_context->pitch; screen->pixels = OCImage.FrameData0; /* emulate 640x400 videomode */ if (videomode_emulatemode==1) { int i; for (i=0; i<40; i++) { SDL_memset(screen->pixels+screen->pitch*i, 0x00, screen->pitch); } for (i=440; i<480; i++) { SDL_memset(screen->pixels+screen->pitch*i, 0x00, screen->pitch); } screen->pixels+=screen->pitch*40; } } this->UpdateRects = ph_OCDCUpdate; /* wait for the hardware */ PgFlush(); PgWaitHWIdle(); return 0;}#if SDL_VIDEO_OPENGLint ph_SetupOpenGLImage(_THIS, SDL_Surface* screen){ this->UpdateRects = ph_OpenGLUpdate; screen->pixels=NULL; screen->pitch=NULL; #if (_NTO_VERSION >= 630) if ((screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) { if (!ph_EnterFullScreen(this, screen, PH_IGNORE_DIRECTMODE)) { screen->flags &= ~SDL_FULLSCREEN; return -1; } } #endif /* 6.3.0 */ if (ph_SetupOpenGLContext(this, screen->w, screen->h, screen->format->BitsPerPixel, screen->flags)!=0) { screen->flags &= ~SDL_OPENGL; return -1; } return 0;}#endif /* SDL_VIDEO_OPENGL */void ph_DestroyImage(_THIS, SDL_Surface* screen){#if SDL_VIDEO_OPENGL if ((screen->flags & SDL_OPENGL)==SDL_OPENGL) { if (oglctx) { #if (_NTO_VERSION < 630) PhDCSetCurrent(NULL); PhDCRelease(oglctx); #else qnxgl_context_destroy(oglctx); qnxgl_buffers_destroy(oglbuffers); qnxgl_finish(); #endif /* 6.3.0 */ oglctx=NULL; oglbuffers=NULL; oglflags=0; oglbpp=0; } #if (_NTO_VERSION >= 630) if (currently_fullscreen) { ph_LeaveFullScreen(this); } #endif /* 6.3.0 */ return; }#endif /* SDL_VIDEO_OPENGL */ if (currently_fullscreen) { /* if we right now in 8bpp fullscreen we must release palette */ if ((screen->format->BitsPerPixel==8) && (desktopbpp!=8)) { PgSetPalette(syspalph, 0, -1, 0, 0, 0); PgSetPalette(savedpal, 0, 0, _Pg_MAX_PALETTE, Pg_PALSET_GLOBAL | Pg_PALSET_FORCE_EXPOSE, 0); PgFlush(); } ph_LeaveFullScreen(this); } if (OCImage.offscreen_context != NULL) { PhDCRelease(OCImage.offscreen_context); OCImage.offscreen_context = NULL; OCImage.FrameData0 = NULL; } if (OCImage.offscreen_backcontext != NULL) { PhDCRelease(OCImage.offscreen_backcontext); OCImage.offscreen_backcontext = NULL; OCImage.FrameData1 = NULL; } OCImage.CurrentFrameData = NULL; if (SDL_Image) { /* if palette allocated, free it */ if (SDL_Image->palette) { SDL_free(SDL_Image->palette); } PgShmemDestroy(SDL_Image->image); SDL_free(SDL_Image); } /* Must be zeroed everytime */ SDL_Image = NULL; if (screen) { screen->pixels = NULL; }}int ph_UpdateHWInfo(_THIS){ PgVideoModeInfo_t vmode; PgHWCaps_t hwcaps; /* Update video ram amount */ if (PgGetGraphicsHWCaps(&hwcaps) < 0) { SDL_SetError("ph_UpdateHWInfo(): GetGraphicsHWCaps() function failed !\n"); return -1; } this->info.video_mem=hwcaps.currently_available_video_ram/1024; /* obtain current mode capabilities */ if (PgGetVideoModeInfo(hwcaps.current_video_mode, &vmode) < 0) { SDL_SetError("ph_UpdateHWInfo(): GetVideoModeInfo() function failed !\n"); return -1; } if ((vmode.mode_capabilities1 & PgVM_MODE_CAP1_OFFSCREEN) == PgVM_MODE_CAP1_OFFSCREEN) { /* this is a special test for drivers which tries to lie about offscreen capability */ if (hwcaps.currently_available_video_ram!=0) { this->info.hw_available = 1; } else { this->info.hw_available = 0; } } else { this->info.hw_available = 0; } if ((vmode.mode_capabilities2 & PgVM_MODE_CAP2_RECTANGLE) == PgVM_MODE_CAP2_RECTANGLE) { this->info.blit_fill = 1; } else { this->info.blit_fill = 0; } if ((vmode.mode_capabilities2 & PgVM_MODE_CAP2_BITBLT) == PgVM_MODE_CAP2_BITBLT) { this->info.blit_hw = 1; } else { this->info.blit_hw = 0; } if ((vmode.mode_capabilities2 & PgVM_MODE_CAP2_ALPHA_BLEND) == PgVM_MODE_CAP2_ALPHA_BLEND) { this->info.blit_hw_A = 1; } else { this->info.blit_hw_A = 0; } if ((vmode.mode_capabilities2 & PgVM_MODE_CAP2_CHROMA) == PgVM_MODE_CAP2_CHROMA) { this->info.blit_hw_CC = 1; } else { this->info.blit_hw_CC = 0; } return 0;}int ph_SetupUpdateFunction(_THIS, SDL_Surface* screen, Uint32 flags){ int setupresult=-1; ph_DestroyImage(this, screen); #if SDL_VIDEO_OPENGL if ((flags & SDL_OPENGL)==SDL_OPENGL) { setupresult=ph_SetupOpenGLImage(this, screen); } else {#endif if ((flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) { setupresult=ph_SetupFullScreenImage(this, screen); } else { if ((flags & SDL_HWSURFACE)==SDL_HWSURFACE) { setupresult=ph_SetupOCImage(this, screen); } else { setupresult=ph_SetupImage(this, screen); } }#if SDL_VIDEO_OPENGL }#endif if (setupresult!=-1) { ph_UpdateHWInfo(this); } return setupresult;}int ph_AllocHWSurface(_THIS, SDL_Surface* surface){ PgHWCaps_t hwcaps; if (surface->hwdata!=NULL) { SDL_SetError("ph_AllocHWSurface(): hwdata already exists!\n"); return -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -