[Xbubble-commits] xbubble-sdl/src game.c, 1.1.1.1,
1.2 graphic_utils.c, 1.1.1.1, 1.2 graphic_utils.h, 1.1.1.1,
1.2 setting.h, 1.3, 1.4 xbubble.c, 1.4, 1.5
Martin Quinson
mquinson at alioth.debian.org
Wed Sep 13 22:51:24 UTC 2006
Update of /cvsroot/xbubble/xbubble-sdl/src
In directory haydn:/tmp/cvs-serv27917
Modified Files:
game.c graphic_utils.c graphic_utils.h setting.h xbubble.c
Log Message:
Use libsdl-gfx to handle the framerate (we already used it for the canon rotation computation)
Index: graphic_utils.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/graphic_utils.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- graphic_utils.c 11 Aug 2006 12:14:42 -0000 1.1.1.1
+++ graphic_utils.c 13 Sep 2006 22:51:22 -0000 1.2
@@ -3,24 +3,36 @@
#include "utils.h"
#include "graphic_utils.h"
+#include "SDL_framerate.h"
+
SDL_Surface *screen;
const char* sdl_error = NULL;
+static int fps_inited = 0;
+static FPSmanager fps_manager;
+unsigned long fps_frame_duration;
/* ****************** */
/* SDL initialization */
/* ****************** */
SDL_Surface *window_make(int width, int height) {
- if (!sdl_error)
- sdl_error = SDL_GetError();
-
/* Initialize a SDL window */
if(SDL_Init(SDL_INIT_VIDEO) < 0)
fail( _("Failed to initialize the SDL library ! %s\n"), SDL_GetError());
atexit(SDL_Quit);
+ /* Other initializations */
+ if (!sdl_error)
+ sdl_error = SDL_GetError();
+
+ if (!fps_inited) {
+ SDL_initFramerate(&fps_manager);
+ fps_set(FPS_DEFAULT);
+ fps_inited = 1;
+ }
+
/* Creating the window */
screen = SDL_SetVideoMode(width, height, 32,
SDL_HWSURFACE|SDL_SRCALPHA|SDL_ANYFORMAT);
@@ -29,45 +41,24 @@
width, height, sdl_error);
/* Window title */
- SDL_WM_SetCaption(_("eXploding Bubble"), NULL);
+ SDL_WM_SetCaption(_("eXploding Bubble (SDL VERSION)"), NULL);
return screen;
}
-#if 0
-void board_display() {
- Uint32 black = SDL_MapRGB(screen->format, 0, 0, 0);
-
-
- SDL_FillRect(screen, NULL, black);
-
- SDL_BlitSurface(bubble.surf, NULL, screen, &pos);
-
- SDL_Flip(screen);
- // printf("Plop %dx%d %dx%d.\n",pos.x,pos.y,speed.x,speed.y);
-
-}
-
+/* ********************* */
+/* Frame rate management */
+/* ********************* */
-/** \brief (internal) Locks or unlocks the surface on need */
-void setLock(SDL_Surface* s,int lock){
- if (SDL_MUSTLOCK(s)){
- if (lock){
- if(SDL_LockSurface(s) < 0)
- warn( _("Unable to lock the surface: %s"), sdl_error);
- } else {
- SDL_UnlockSurface(s);
- }
- }
+int fps_get(void) {
+ return SDL_getFramerate(&fps_manager);
}
-/** \brief Locks the surface on need */
-void lockSurface(SDL_Surface* w){
- setLock(w,1);
+void fps_set(int fps) {
+ SDL_setFramerate(&fps_manager,clip(fps, FPS_LOWER_LIMIT, FPS_UPPER_LIMIT));
+ fps_frame_duration = 1000/fps_manager.rate;
}
-/** \brief Unlocks the surface on need */
-void unlockSurface(SDL_Surface* w){
- setLock(w,0);
+void fps_delay(void) {
+ SDL_framerateDelay(&fps_manager);
}
-#endif
Index: game.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/game.c,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- game.c 11 Aug 2006 12:14:49 -0000 1.1.1.1
+++ game.c 13 Sep 2006 22:51:22 -0000 1.2
@@ -38,6 +38,8 @@
#include "game.h"
#include "cell.h"
+#include "graphic_utils.h"
+
/* FIXME
extern Display *display;
extern Window root;
@@ -443,7 +445,7 @@
game->need_restart_thinking = 1;
game->computer_state[n] = opponent_idle;
}
- game->computer_thinking_time[n] -= frame_duration/1000;
+ game->computer_thinking_time[n] -= fps_frame_duration/1000;
/* think only when board is stable */
if ( board_get_state( game->board[n] ) == board_state_ready_to_fire ) {
@@ -479,7 +481,7 @@
else
canon_rotate_left( game->board[n] );
/* if necessary, fine tune canon rotation ( yes that's cheating ) */
- if ( abs(shift) < ( frame_duration/1000.0 )*CANON_ROTATING_SPEED ) {
+ if ( abs(shift) < fps_get()*CANON_ROTATING_SPEED ) { /* was "( frame_duration/1000.0 )" instead of fps_get() */
canon_move( game->board[n],
(int) floor( abs(shift)/CANON_ROTATING_SPEED ));
canon_stop( game->board[n] );
@@ -609,7 +611,7 @@
default:
break;
}
- animate_game( game, frame_duration );
+ animate_game( game, fps_frame_duration );
/* check if game is not over */
if ( game->multi_player ) {
@@ -665,7 +667,7 @@
( board_get_state( game->board[1] ) == board_state_frozen )))
game->state = game_state_finished;
else
- animate_game( game, frame_duration/1000 );
+ animate_game( game, fps_frame_duration );
break;
default:
@@ -768,10 +770,6 @@
game_msg_hide(game);
}
- int sec=0;
- int fps=0;
- int inter_frame_delay = 20;
- int clock = begin;
game->state = game_state_playing;
do {
@@ -790,17 +788,7 @@
}
}
- /* Compute FPS and have a rest if we're too fast */
- fps++;
- Uint32 now=SDL_GetTicks();
- if (now / 1000 == sec+1) {
- printf("%d fps\n",fps);
- fps=0;
- sec++;
- }
- if (frame_duration > now-clock)
- SDL_Delay(frame_duration - now + clock);
- clock=now;
+ fps_delay();
/* Update the game */
frame_update();
Index: xbubble.c
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/xbubble.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- xbubble.c 12 Sep 2006 08:04:08 -0000 1.4
+++ xbubble.c 13 Sep 2006 22:51:22 -0000 1.5
@@ -7,7 +7,6 @@
#include "game.h"
#include "cell.h"
-unsigned long frame_duration=20;
SDL_Surface *screen;
int main(int argc, char *argv[]) {
int zoom = DEFAULT_SCALE;
Index: graphic_utils.h
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/graphic_utils.h,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- graphic_utils.h 11 Aug 2006 12:14:42 -0000 1.1.1.1
+++ graphic_utils.h 13 Sep 2006 22:51:22 -0000 1.2
@@ -8,9 +8,10 @@
SDL_Surface *window_make(int width, int height);
-void board_display(void);
-
-void surf_lock(SDL_Surface* w);
-void surf_unlock(SDL_Surface* w);
+/* to compute the length of an animation, do <nb_sec> * fps_get() */
+int fps_get(void);
+void fps_set(int fps);
+void fps_delay(void);
+unsigned long fps_frame_duration; /* in ms */
#endif /* H_GRAPHIC_UTILS */
Index: setting.h
===================================================================
RCS file: /cvsroot/xbubble/xbubble-sdl/src/setting.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- setting.h 11 Sep 2006 21:33:19 -0000 1.3
+++ setting.h 13 Sep 2006 22:51:22 -0000 1.4
@@ -22,8 +22,6 @@
#define OFFSET_X ( 1.0*SIDE_WIDTH/MAX_SCALE )
#define OFFSET_Y ( 1.0*FRAME_HEIGHT/MAX_SCALE )
-//#define board2screen_x(X,board) (X+OFFSET_X)*(board->scale)+board->offset.x
-//#define board2screen_y(Y,board) (Y+OFFSET_Y)*(board->scale)+board->offset.y
#define board2screen_x(X,board) ((X)*(board->scale)+board->offset.x)
#define board2screen_y(Y,board) ((Y)*(board->scale)+board->offset.y)
#define board2screen(X,Y,board) board2screen_x(X,board), board2screen_y(Y,board)
@@ -82,7 +80,6 @@
#define MAX_NB_LEVELS 100
/* game screens */
-#define DEFAULT_FPS 80
#define MIN(a,b) ((a)<(b)? (a):(b))
#define MAX(a,b) ((a)>(b)? (a):(b))
More information about the Xbubble-commits
mailing list