00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <string.h>
00021 #include <flgrCoreMalloc.h>
00022 #include <flgrCoreDispatch.h>
00023 #include "flgrRtView.h"
00024
00032
00033
00039
00040 FLGR_RtView2D *flgr2d_rtview_create(FLGR_Data2D *data, int size_yfactor, int size_xfactor) {
00041 FLGR_RtView2D *screen;
00042 int bps;
00043 int i;
00044
00045
00046
00047 if(data==NULL){
00048 POST_ERROR("Null objects!\n");
00049 return NULL;
00050 }
00051
00052 if( SDL_Init( SDL_INIT_VIDEO ) == -1 ) {
00053 POST_ERROR("Could not init SDL: %s\n", SDL_GetError( ) );
00054 return NULL;
00055 }
00056
00057 bps = data->spp * data->bps;
00058
00059 if((bps != 8) && (bps != 24) && (bps != 32)) {
00060 POST_ERROR("bps or spp invalid\n");
00061 return NULL;
00062 }
00063
00064 screen = flgr_malloc(sizeof(FLGR_RtView2D));
00065
00066
00067 screen->size_xfactor = size_xfactor;
00068 screen->size_yfactor = size_yfactor;
00069 screen->bps = bps;
00070
00071 atexit( SDL_Quit );
00072 screen->surface = SDL_SetVideoMode( data->size_x*size_xfactor, data->size_y*size_yfactor,
00073 bps, SDL_HWSURFACE );
00074 SDL_WM_SetCaption("RtView", NULL );
00075
00076 if(bps==8) {
00077 for(i=0;i<256;i++) {
00078 screen->palette[i].r=i;
00079 screen->palette[i].g=i;
00080 screen->palette[i].b=i;
00081 }
00082 SDL_SetPalette(screen->surface, SDL_LOGPAL|SDL_PHYSPAL, screen->palette, 0, 256);
00083 }else if(bps==24) {
00084 screen->surface->format->Rmask = 0x000000FF;
00085 screen->surface->format->Gmask = 0x0000FF00;
00086 screen->surface->format->Bmask = 0x00FF0000;
00087 screen->surface->format->Amask = 0xFF000000;
00088 screen->surface->format->Rshift = 0;
00089 screen->surface->format->Gshift = 8;
00090 screen->surface->format->Bshift = 16;
00091 screen->surface->format->Ashift = 24;
00092 screen->surface->format->Rloss = 0;
00093 screen->surface->format->Gloss = 0;
00094 screen->surface->format->Bloss = 0;
00095 screen->surface->format->Aloss = 0;
00096 }
00097
00098 if( screen == NULL ) {
00099 POST_ERROR( "Can't set video mode: %s\n", SDL_GetError( ) );
00100 return NULL;
00101 }
00102
00103 return screen;
00104
00105 }
00106
00107
00108
00110
00117
00118 FLGR_Ret flgr2d_rtview_refresh(FLGR_RtView2D *screen, FLGR_Data2D *data, int size_ypos, int size_xpos) {
00119 int i;
00120 int Bps = data->spp * (data->bps>>3);
00121 int w = data->size_x;
00122 int h = data->size_y;
00123 int wfact = screen->size_xfactor;
00124 int hfact = screen->size_yfactor;
00125 void *psf = screen->surface->pixels + w*size_xpos*Bps + h*size_ypos*w*wfact*Bps;
00126
00127
00128
00129 if((screen==NULL) || (data==NULL)){
00130 POST_ERROR("Null objects!\n");
00131 return FLGR_RET_NULL_OBJECT;
00132 }
00133
00134
00135 if(size_xpos>=wfact) {
00136 POST_WARNING("Width position out of the screen window");
00137 size_xpos = screen->size_xfactor-1;
00138 }
00139
00140 if(size_ypos>=hfact) {
00141 POST_WARNING("Height position out of the screen window");
00142 size_ypos = screen->size_yfactor-1;
00143 }
00144
00145 if(SDL_MUSTLOCK(screen->surface)) {
00146 if(SDL_LockSurface( screen->surface )<0) {
00147 POST_ERROR("Could not Lock SDL surface\n");
00148 return FLGR_RET_UNDEFINED_ERROR;
00149 }
00150 }
00151
00152 for(i=0;i<h;i++) {
00153 memcpy(psf, data->array[i], w*Bps);
00154 psf+=w*Bps*wfact;
00155 }
00156
00157 if(SDL_MUSTLOCK(screen->surface)) {
00158 SDL_UnlockSurface( screen->surface );
00159 }
00160
00161 if( SDL_Flip( screen->surface ) == -1 ) {
00162 POST_ERROR("Could not refresh SDL window\n");
00163 return FLGR_RET_UNDEFINED_ERROR;
00164 }
00165 SDL_UpdateRect(screen->surface, 0, 0, w, h);
00166 return FLGR_RET_OK;
00167 }
00168
00170
00174
00175 void flgr2d_rtview_destroy(FLGR_RtView2D *screen) {
00176
00177
00178 SDL_FreeSurface(screen->surface);
00179 flgr_free(screen);
00180 SDL_Quit();
00181 }
00182
00183
00185
00192
00193 FLGR_Ret flgr2d_rtview_set_palette(FLGR_RtView2D *screen, int *rTable, int *gTable, int *bTable) {
00194 int i;
00195
00196
00197
00198 if(screen==NULL){
00199 POST_ERROR("Null objects!\n");
00200 return FLGR_RET_NULL_OBJECT;
00201 }
00202
00203
00204 if(screen->bps != 8) {
00205 POST_ERROR("Pallete could only be used with 8 bits display!\n");
00206 return FLGR_RET_UNDEFINED_ERROR;
00207 }
00208
00209 for(i=0 ; i<256 ; i++) {
00210 screen->palette[i].r = rTable[i];
00211 screen->palette[i].g = gTable[i];
00212 screen->palette[i].b = bTable[i];
00213 }
00214
00215
00216 SDL_SetPalette(screen->surface, SDL_LOGPAL|SDL_PHYSPAL, screen->palette, 0, 256);
00217
00218
00219 return FLGR_RET_OK;
00220 }
00221
00222