mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	framerate keeper for main loop, working wersion of color converter (blue -> player's) and small tweak
This commit is contained in:
		
							
								
								
									
										22
									
								
								CMT.cpp
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								CMT.cpp
									
									
									
									
									
								
							| @@ -6,6 +6,7 @@ | ||||
| #include "SDL_mixer.h" | ||||
| #include "CBuildingHandler.h" | ||||
| #include "SDL_Extensions.h" | ||||
| #include "SDL_framerate.h" | ||||
| #include <cmath> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| @@ -267,6 +268,9 @@ int _tmain(int argc, _TCHAR* argv[]) | ||||
| 		THC std::cout<<"Loading .lods: "<<tmh.getDif()<<std::endl; | ||||
| 		CPreGame * cpg = new CPreGame(); //main menu and submenus | ||||
| 		THC std::cout<<"Initialization CPreGame (together): "<<tmh.getDif()<<std::endl; | ||||
| 		//CMessage * ll = new CMessage; | ||||
| 		//CSDL_Ext::blueToPlayersAdv(ll->piecesOfBox[0].ourImages[0].bitmap, 0); | ||||
| 		//SDL_SaveBMP(ll->piecesOfBox[0].ourImages[0].bitmap, "test2.bmp"); | ||||
| 		cpg->mush = mush; | ||||
| 		cpg->runLoop(); | ||||
| 		THC tmh.getDif(); | ||||
| @@ -374,7 +378,13 @@ int _tmain(int argc, _TCHAR* argv[]) | ||||
| 		bool scrollingUp = false; | ||||
| 		bool scrollingDown = false; | ||||
| 		bool updateScreen = false; | ||||
| 		unsigned char animVal=0; //for animations handling | ||||
| 		unsigned char animVal = 0; //for animations handling | ||||
| 		unsigned char animValHitCount = 0; //for slower animations | ||||
| 		//initializing framerate keeper | ||||
| 		FPSmanager * mainLoopFramerateKeeper = new FPSmanager; | ||||
| 		SDL_initFramerate(mainLoopFramerateKeeper); | ||||
| 		SDL_setFramerate(mainLoopFramerateKeeper, 30); | ||||
| 		//framerate keeper initialized | ||||
| 		for(;;) // main loop | ||||
| 		{ | ||||
| 			try | ||||
| @@ -535,9 +545,15 @@ int _tmain(int argc, _TCHAR* argv[]) | ||||
| 			{ continue; } | ||||
|  | ||||
| 			updateScreen = true; | ||||
| 			++animVal; //for animations | ||||
| 			++animValHitCount; //for animations | ||||
| 			if(animValHitCount == 2) | ||||
| 			{ | ||||
| 				animValHitCount = 0; | ||||
| 				++animVal; | ||||
| 			} | ||||
|  | ||||
| 			SDL_Delay(30); //give time for other apps | ||||
| 			SDL_Delay(5); //give time for other apps | ||||
| 			SDL_framerateDelay(mainLoopFramerateKeeper); | ||||
| 		} | ||||
| 		return 0; | ||||
| 	} | ||||
|   | ||||
| @@ -514,13 +514,70 @@ void CSDL_Ext::blueToPlayersAdv(SDL_Surface * sur, int player) | ||||
| 					std::swap(sort2[0], sort2[1]); | ||||
| 				for(int hh=0; hh<3; ++hh) | ||||
| 				{ | ||||
| 					(*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2; | ||||
| 					(*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| 	else if(sur->format->BitsPerPixel == 24) | ||||
| 	{ | ||||
| 		for(int y=0; y<sur->h; ++y) | ||||
| 		{ | ||||
| 			for(int x=0; x<sur->w; ++x) | ||||
| 			{ | ||||
| 				Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3; | ||||
| 				if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | ||||
| 				{ | ||||
| 					if(cp[2]>cp[1] && cp[2]>cp[0]) | ||||
| 					{ | ||||
| 						std::vector<long long int> sort1; | ||||
| 						sort1.push_back(cp[0]); | ||||
| 						sort1.push_back(cp[1]); | ||||
| 						sort1.push_back(cp[2]); | ||||
| 						std::vector< std::pair<long long int, Uint8*> > sort2; | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[0]))); | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1]))); | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[2]))); | ||||
| 						std::sort(sort1.begin(), sort1.end()); | ||||
| 						if(sort2[0].first>sort2[1].first) | ||||
| 							std::swap(sort2[0], sort2[1]); | ||||
| 						if(sort2[1].first>sort2[2].first) | ||||
| 							std::swap(sort2[1], sort2[2]); | ||||
| 						if(sort2[0].first>sort2[1].first) | ||||
| 							std::swap(sort2[0], sort2[1]); | ||||
| 						for(int hh=0; hh<3; ++hh) | ||||
| 						{ | ||||
| 							(*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2; | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 				else | ||||
| 				{ | ||||
| 					if(cp[0]>cp[1] && cp[0]>cp[2]) | ||||
| 					{ | ||||
| 						std::vector<long long int> sort1; | ||||
| 						sort1.push_back(cp[2]); | ||||
| 						sort1.push_back(cp[1]); | ||||
| 						sort1.push_back(cp[0]); | ||||
| 						std::vector< std::pair<long long int, Uint8*> > sort2; | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[2]))); | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1]))); | ||||
| 						sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[0]))); | ||||
| 						std::sort(sort1.begin(), sort1.end()); | ||||
| 						if(sort2[0].first>sort2[1].first) | ||||
| 							std::swap(sort2[0], sort2[1]); | ||||
| 						if(sort2[1].first>sort2[2].first) | ||||
| 							std::swap(sort2[1], sort2[2]); | ||||
| 						if(sort2[0].first>sort2[1].first) | ||||
| 							std::swap(sort2[0], sort2[1]); | ||||
| 						for(int hh=0; hh<3; ++hh) | ||||
| 						{ | ||||
| 							(*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2; | ||||
| 						} | ||||
| 					} | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
							
								
								
									
										84
									
								
								SDL_framerate.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								SDL_framerate.c
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,84 @@ | ||||
|  | ||||
| /* | ||||
|  | ||||
|  SDL_framerate: framerate manager | ||||
|  | ||||
|  LGPL (c) A. Schiffler | ||||
|   | ||||
|  */ | ||||
|  | ||||
| #include "SDL_framerate.h" | ||||
|  | ||||
| /*  | ||||
|    Initialize the framerate manager | ||||
| */ | ||||
|  | ||||
| void SDL_initFramerate(FPSmanager * manager) | ||||
| { | ||||
|     /* | ||||
|      * Store some sane values  | ||||
|      */ | ||||
|     manager->framecount = 0; | ||||
|     manager->rate = FPS_DEFAULT; | ||||
|     manager->rateticks = (1000.0 / (float) FPS_DEFAULT); | ||||
|     manager->lastticks = SDL_GetTicks(); | ||||
| } | ||||
|  | ||||
| /*  | ||||
|    Set the framerate in Hz  | ||||
| */ | ||||
|  | ||||
| int SDL_setFramerate(FPSmanager * manager, int rate) | ||||
| { | ||||
|     if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) { | ||||
| 	manager->framecount = 0; | ||||
| 	manager->rate = rate; | ||||
| 	manager->rateticks = (1000.0 / (float) rate); | ||||
| 	return (0); | ||||
|     } else { | ||||
| 	return (-1); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /*  | ||||
|   Return the current target framerate in Hz  | ||||
| */ | ||||
|  | ||||
| int SDL_getFramerate(FPSmanager * manager) | ||||
| { | ||||
|     if (manager == NULL) { | ||||
| 	return (-1); | ||||
|     } else { | ||||
| 	return (manager->rate); | ||||
|     } | ||||
| } | ||||
|  | ||||
| /*  | ||||
|   Delay execution to maintain a constant framerate. Calculate fps. | ||||
| */ | ||||
|  | ||||
| void SDL_framerateDelay(FPSmanager * manager) | ||||
| { | ||||
|     Uint32 current_ticks; | ||||
|     Uint32 target_ticks; | ||||
|     Uint32 the_delay; | ||||
|  | ||||
|     /* | ||||
|      * Next frame  | ||||
|      */ | ||||
|     manager->framecount++; | ||||
|  | ||||
|     /* | ||||
|      * Get/calc ticks  | ||||
|      */ | ||||
|     current_ticks = SDL_GetTicks(); | ||||
|     target_ticks = manager->lastticks + (Uint32) ((float) manager->framecount * manager->rateticks); | ||||
|  | ||||
|     if (current_ticks <= target_ticks) { | ||||
| 	the_delay = target_ticks - current_ticks; | ||||
| 	SDL_Delay(the_delay); | ||||
|     } else { | ||||
| 	manager->framecount = 0; | ||||
| 	manager->lastticks = SDL_GetTicks(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										65
									
								
								SDL_framerate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								SDL_framerate.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
|  | ||||
| /* | ||||
|  | ||||
|  SDL_framerate: framerate manager | ||||
|   | ||||
|  LGPL (c) A. Schiffler | ||||
|   | ||||
|  */ | ||||
|  | ||||
| #ifndef _SDL_framerate_h | ||||
| #define _SDL_framerate_h | ||||
|  | ||||
| /* Set up for C function definitions, even when using C++ */ | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| /* --- */ | ||||
|  | ||||
| #include "SDL.h" | ||||
|  | ||||
| /* --------- Definitions */ | ||||
|  | ||||
| /* Some rates in Hz */ | ||||
|  | ||||
| #define FPS_UPPER_LIMIT		200 | ||||
| #define FPS_LOWER_LIMIT		1 | ||||
| #define FPS_DEFAULT		30 | ||||
|  | ||||
| /* --------- Structure variables */ | ||||
|  | ||||
| 	typedef struct { | ||||
| 	Uint32 framecount; | ||||
| 	float rateticks; | ||||
| 	Uint32 lastticks; | ||||
| 	Uint32 rate; | ||||
| 	} FPSmanager; | ||||
|  | ||||
| /* --------- Function prototypes */ | ||||
|  | ||||
| #ifdef WIN32 | ||||
| #ifdef BUILD_DLL | ||||
| #define DLLINTERFACE __declspec(dllexport) | ||||
| #else | ||||
| #define DLLINTERFACE __declspec(dllimport) | ||||
| #endif | ||||
| #else | ||||
| #define DLLINTERFACE | ||||
| #endif | ||||
|  | ||||
| /* Functions return 0 or value for sucess and -1 for error */ | ||||
|  | ||||
| 	void SDL_initFramerate(FPSmanager * manager); | ||||
| 	int SDL_setFramerate(FPSmanager * manager, int rate); | ||||
| 	int SDL_getFramerate(FPSmanager * manager); | ||||
| 	void SDL_framerateDelay(FPSmanager * manager); | ||||
|  | ||||
| /* --- */ | ||||
|  | ||||
| /* Ends C function definitions when using C++ */ | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif				/* _SDL_framerate_h */ | ||||
		Reference in New Issue
	
	Block a user