src/SAL_render.cpp

00001 /*
00002     SAL - Simple Application Library
00003     Copyright (C) 2006-2006 Kronon
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Lesser General Public
00007     License as published by the Free Software Foundation; either
00008     version 2.1 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Lesser General Public License for more details.
00014 
00015     You should have received a copy of the GNU Lesser General Public
00016     License along with this library; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018 
00019     Kronon
00020     kronon88@users.sourceforge.net
00021 */
00022 
00023 //SAL render functions
00024 //Created by Kronon
00025 
00026 #include "SAL_render.h"
00027 
00028 //Doxy gen information
00029 
00041 int SAL_render_thread(void *unused)
00042 {
00043         #ifdef SAL_DEBUG_ON
00044         sleep(1);       //Debuger crashes in some case because it can't find the thread after termination
00045                 if(SAL_struct.internal.render.display == NULL)
00046                 {
00047                         salError("Error: The display isn't set yet");
00048                         return -1;
00049                 }
00050                 if(SAL_struct.setting.render.refresh_rate == 0)
00051                 {
00052                         printf("Event driven renderer selected.\n");
00053                         return 0;
00054                 }
00055     #endif
00056     
00057                 if(SAL_struct.setting.render.refresh_rate == 0)
00058                 {
00059                         #ifdef SAL_DEBUG_ON
00060                                 salError("Not using any refresh rate");
00061                         #endif
00062                         return 0;
00063                 }
00064                 //FIXME: should be in the loadable render mode files
00065                 while(SAL_struct.internal.checks.alive)
00066                 {   
00067 //             while(SAL_struct.internal.render.lock)            //Not perfect solution but it won't (almost never) render while drawing
00068 //             {
00069 //                 SDL_Delay(7);
00070 //             }
00071                         SDL_Flip(SAL_struct.internal.render.display);
00072                         SDL_Delay(SAL_struct.setting.render.refresh_rate);
00073                 }
00074         return 0;
00075 }
00076 
00083 int salRefresh()
00084 {
00086         SDL_Rect dest;                                                      //The coordinates where the image will be drawn
00087         dest.w = 0;
00088         dest.h = 0;
00089         SAL_gui_object* gui_object;                                                                                             //Object that needs to be updated
00090         //Redraw buttons
00091         for(SAL_struct.internal.event.visable_objects.setLoopBegin(); SAL_struct.internal.event.visable_objects.inLoop(); )
00092         {
00093                 gui_object = SAL_struct.internal.event.visable_objects.getLoopEntry();
00094                 dest.x = gui_object->x;
00095                 dest.y = gui_object->y;
00096                 if ( SDL_MUSTLOCK(SAL_struct.internal.render.display) )                             //Lock structure
00097                 {
00098                         if ( SDL_LockSurface(SAL_struct.internal.render.display) < 0 )
00099                         {
00100                                 return -1;
00101                         }
00102                 }
00103                 //Blit the image to the display
00104                 
00105                 SDL_BlitSurface(gui_object->surface,NULL,SAL_struct.internal.render.display,&dest);
00106                 if ( SDL_MUSTLOCK(SAL_struct.internal.render.display) )                             //Unlock structure
00107                 {
00108                         SDL_UnlockSurface(SAL_struct.internal.render.display);
00109                 }
00110         }
00111         return SDL_Flip(SAL_struct.internal.render.display);
00112 }
00113 
00118 int SAL_start_renderer()
00119 {
00120         SAL_struct.internal.render.thread = SDL_CreateThread(SAL_render_thread, NULL);
00121         #ifdef SAL_DEBUG_ON
00122                 if(SAL_struct.internal.render.thread == NULL)
00123                 {
00124                         salError("Error: Render thread couldn't start");
00125                         return -1;
00126                 }
00127     #endif
00128     return 0;
00129 }