Skip to content

Commit

Permalink
Décorrelation FPS/horloge jeu
Browse files Browse the repository at this point in the history
  • Loading branch information
lfgaleota committed May 6, 2024
1 parent 830786c commit e7bdd7c
Show file tree
Hide file tree
Showing 16 changed files with 149 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ set(SOURCE_FILES main.c init.c inc/init.h circular_menu.c inc/circular_menu.h in
add_executable(ECE-Sprits ${SOURCE_FILES})

find_library(fmodex fmodex lib/fmodex)
target_link_libraries(ECE-Sprits m z png loadpng jpgalleg alleg44 ${fmodex})
target_link_libraries(ECE-Sprits m z png loadpng jpgalleg fblend alleg44 ${fmodex})
2 changes: 2 additions & 0 deletions capacities.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ char Capacities_setDirection( Level* level, Object* obj ) {
blit( level->bmps.page, screen, 0, 0, 0, 0, level->bmps.page->w, level->bmps.page->h );
}

level->before = clock();

return done - 1;
}

Expand Down
1 change: 1 addition & 0 deletions fmod.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void FMod_init( FMod* fmod ) {
FMOD_System_GetMasterChannelGroup( fmod->sys, &fmod->channel_master );
FMOD_System_GetChannel( fmod->sys, 1, &fmod->channel_1 );
FMOD_System_GetChannel( fmod->sys, 2, &fmod->channel_2 );
//FMOD_ChannelGroup_SetVolume( fmod->channel_master, 0 );
}

char FMod_load( FMod* fmod ) {
Expand Down
142 changes: 122 additions & 20 deletions game.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
#include "inc/game.h"

int fps, fixedfps;

void fpser() {
fixedfps = fps;
fps = 0;
}
END_OF_FUNCTION( fpser )

void FMod_generateWave( FMod* fmod, BITMAP* dest ) {
int i;
const int numberOfSamples = 16384;
float wave[ numberOfSamples ];
FMOD_System_GetWaveData( fmod->sys, &wave[ 0 ], numberOfSamples, 0 );

clear_to_color( dest, makecol( 255, 0, 255 ) );

for( i = 0; i < numberOfSamples; ++i ) {
if( i > 0 )
line( dest, (float) SCREEN_W / numberOfSamples * ( i - 1 ), SCREEN_H / 2 + SCREEN_H / 3 * wave[ i - 1 ], (float) SCREEN_W / numberOfSamples * i, SCREEN_H / 2 + SCREEN_H / 3 * wave[ i ], makecol( 255, 255, 255 ) );
}
}

void Game_pauseMenu( Level* level ) {
int choice = 0;

Expand Down Expand Up @@ -49,6 +71,8 @@ void Game_pauseMenu( Level* level ) {
default:
break;
}

level->before = clock();
}

void Game_showFlame( Level* level, Object* obj ) {
Expand Down Expand Up @@ -80,15 +104,42 @@ void Game_showBlow( Level* level, Object* obj ) {
}

void Game_showUI( Level* level ) {
BITMAP* current;
if( level->counter > 1 )
level->ui_counter++;

if( level->ui_counter > UI_COUNTER_MAX ) {
level->ui_counter = 0;
level->ui_frames++;

if( level->ui_frames >= level->bmps.ui_accelerate.count )
level->ui_frames = 0;
}

rectfill( level->bmps.page, 0, 0, UI_WIDTH, SCREEN_H, makecol( 255, 255, 255 ) );
rectfill( level->bmps.page, UI_WIDTH, 0, UI_WIDTH + 1, SCREEN_H, makecol( 0, 0, 0 ) );

blit( level->bmps.ui_accelerate, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, UI_PADDING_Y, UI_BUTTON_X, UI_BUTTON_Y );
blit( level->bmps.ui_pause, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, 2 * UI_PADDING_Y + UI_BUTTON_Y, UI_BUTTON_X, UI_BUTTON_Y );
if( level->speed == LEVEL_SPEED_FAST )
current = level->bmps.ui_accelerate.bmps[ level->ui_frames ];
else
current = level->bmps.ui_accelerate.bmps[ 0 ];
blit( current, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, UI_PADDING_Y, UI_BUTTON_X, UI_BUTTON_Y );

blit( level->bmps.ui_menu, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, SCREEN_H - UI_PADDING_Y - UI_BUTTON_Y, UI_BUTTON_X, UI_BUTTON_Y );
if( level->speed == LEVEL_SPEED_NONE )
current = level->bmps.ui_pause.bmps[ 1 ];
else
current = level->bmps.ui_pause.bmps[ 0 ];
blit( current, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, 2 * UI_PADDING_Y + UI_BUTTON_Y, UI_BUTTON_X, UI_BUTTON_Y );

if( level->win )
current = level->bmps.ui_menu.bmps[ level->ui_frames ];
else
current = level->bmps.ui_menu.bmps[ 0 ];
blit( current, level->bmps.page, 0, 0, ( UI_WIDTH - UI_BUTTON_X ) / 2, SCREEN_H - UI_PADDING_Y - UI_BUTTON_Y, UI_BUTTON_X, UI_BUTTON_Y );

textprintf_centre_ex( level->bmps.page, level->bmps.droidsans_14_mono, UI_WIDTH / 2, 3 * UI_PADDING_Y + 2 * UI_BUTTON_Y, makecol( 0, 0, 0 ), -1, "V: %0.1f", level->dt );

textprintf_centre_ex( level->bmps.page, level->bmps.droidsans_14_mono, UI_WIDTH / 2, 3 * UI_PADDING_Y + 2 * UI_BUTTON_Y + font->height + 10, makecol( 0, 0, 0 ), -1, "FPS: %d", fixedfps );

draw_trans_sprite( level->bmps.page, level->bmps.stickmen_walking.bmps[ 0 ], ( UI_WIDTH - level->bmps.stickmen_walking.bmps[ 0 ]->w ) / 2, SCREEN_H / 4 );
textprintf_centre_ex( level->bmps.page, level->bmps.droidsans_14_mono, UI_WIDTH / 2, SCREEN_H / 4 + UI_PADDING_Y + level->bmps.stickmen_walking.bmps[ 0 ]->h, makecol( 0, 0, 0 ), -1, "%d", level->nb_stickmen_arrived );
Expand Down Expand Up @@ -143,8 +194,14 @@ void Game_show( Level* level ) {

CircularMenu_show( level->capacities_menu, level->bmps.scrolling_page, level->inputs.scrolling_mouse.x, level->inputs.scrolling_mouse.y );

/*FMod_generateWave( level->fmod, level->bmps.wave );
masked_blit( level->bmps.wave, level->bmps.page, 0, 0, 0, 0, SCREEN_W, SCREEN_H );*/

blit( level->bmps.scrolling_page, level->bmps.page, level->scrolling.x, level->scrolling.y, UI_WIDTH, 0, SCREEN_W, SCREEN_H );

//fblend_trans( level->bmps.wave, level->bmps.page, 0, 0, 255 * 0.3 );

Game_showUI( level );

blit( level->bmps.page, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H );
Expand Down Expand Up @@ -183,7 +240,9 @@ void Game_updateObjectProperties( Level* level, Object* obj ) {
}
}

obj->counter++;
if( level->counter > 1 )
obj->counter++;

switch( obj->state ) {

case STATE_FALLDYING:
Expand Down Expand Up @@ -251,7 +310,8 @@ void Game_addStickmen( Level* level ) {
Object* obj;

if( level->nb_stickmen_added < level->nb_stickmen_max ) {
level->counter_stickmen_arrival++;
if( level->counter > 1 )
level->counter_stickmen_arrival++;

if( level->counter_stickmen_arrival > level->counter_stickmen_arrival_max ) {
obj = Object_allocate();
Expand Down Expand Up @@ -297,6 +357,8 @@ void Game_handleObjectClick( Level* level, Object* obj ) {
ObjectM *maillon;
char ret = 0;

int oldfixedfps = fixedfps, oldfps = fps;

if( key[ KEY_ESC ] ) {
Game_pauseMenu( level );
}
Expand Down Expand Up @@ -325,6 +387,9 @@ void Game_handleObjectClick( Level* level, Object* obj ) {
}
}
}

fixedfps = oldfixedfps;
fps = oldfps;
}

void Game_handleInputs( Level* level ) {
Expand Down Expand Up @@ -357,17 +422,17 @@ void Game_handleInputs( Level* level ) {

if( !level->inputs.prev_mouse_l && level->inputs.mouse_l ) {
if( mouse_x >= 0 && mouse_x <= UI_WIDTH && mouse_y >= 0 && mouse_y <= UI_PADDING_Y + UI_BUTTON_Y ) {
if( level->dt == level->fast_dt ) {
level->dt = level->slow_dt;
if( level->speed == LEVEL_SPEED_SLOW ) {
level->speed = LEVEL_SPEED_FAST;
} else {
level->dt = level->fast_dt;
level->speed = LEVEL_SPEED_SLOW;
}
} else if( mouse_x >= 0 && mouse_x <= UI_WIDTH && mouse_y > UI_PADDING_Y + UI_BUTTON_Y &&
mouse_y <= 2 * UI_PADDING_Y + 2 * UI_BUTTON_Y ) {
if( level->dt == 0 ) {
level->dt = level->slow_dt;
if( level->speed == LEVEL_SPEED_NONE ) {
level->speed = LEVEL_SPEED_SLOW;
} else {
level->dt = 0;
level->speed = LEVEL_SPEED_NONE;
Game_show( level );
}
} else if( mouse_x >= 0 && mouse_x <= UI_WIDTH && mouse_y >= SCREEN_H - UI_BUTTON_Y - UI_PADDING_Y &&
Expand Down Expand Up @@ -479,6 +544,12 @@ char Game_createBitmaps( Level* level ) {
return 0;
}

level->bmps.wave = create_bitmap( SCREEN_W, SCREEN_H );
if( !level->bmps.wave ) {
allegro_message( "Erreur creation bitmap" );
return 0;
}

level->bmps.scrolling_page = create_bitmap( level->bmps.col->w, level->bmps.col->h );
if( !level->bmps.scrolling_page ) {
allegro_message( "Erreur creation bitmap" );
Expand Down Expand Up @@ -549,6 +620,14 @@ char Game_levelInit( Level* level ) {
level->win = 0;
level->quit = 0;

level->speed = LEVEL_SPEED_SLOW;

level->before = clock();
level->counter = 0;

level->ui_counter = 0;
level->ui_frames = 0;

level->inputs.prev_mouse_l = 0;
level->inputs.mouse_l = 0;

Expand All @@ -572,38 +651,61 @@ void Game_free( Level* level ) {
ObjectM_freeAll( level->stickmen, 1 );

destroy_bitmap( level->bmps.page );
destroy_bitmap( level->bmps.wave );
destroy_bitmap( level->bmps.scrolling_page );
destroy_bitmap( level->bmps.stick_col );
destroy_bitmap( level->bmps.wind_col );
}

char Game_launch( Level* level ) {
float dt;

if( !Game_levelInit( level ) ) {
allegro_message( "Impossible de lancer le niveau!" );
return QUIT_DELIBERATE;
}

FMod_playMusic( level->fmod, level->fmod->music );

fps = 0;
fixedfps = 30;

LOCK_VARIABLE( fps );
LOCK_VARIABLE( fixedfps );
LOCK_FUNCTION( fpser );

install_int( fpser, 10 );
install_int_ex( fpser, SECS_TO_TIMER( 1 ) );

while( !level->quit ) {
dt = (float) ( clock() - level->before ) / 30;

if( key[ KEY_SPACE ] ) {
if( level->dt == level->slow_dt ) {
level->dt = level->fast_dt;
} else if( level->dt == level->fast_dt ) {
level->dt = 0;
} else {
level->dt = level->slow_dt;
}
level->counter += dt;

if( level->speed == LEVEL_SPEED_NONE ) {
level->dt = 0;
} else if( level->speed == LEVEL_SPEED_FAST ) {
level->dt = level->fast_dt * dt;
} else {
level->dt = level->slow_dt * dt;
}

level->before = clock();

Game_update( level );

if( level->dt > 0 )
Game_show( level );

SLEEP( 1 / 30.0 * 1000 ); // 30 FPS idéalement
if( level->counter > 1 )
level->counter = 0;

fps++;

//SLEEP( 1 / 30.0 * 1000 ); // 30 FPS idéalement
}

remove_int( fpser );

FMod_stopMusic( level->fmod );

Expand Down
Binary file removed images/acc.png
Binary file not shown.
Binary file added images/acc1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/acc2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/menu.png
Binary file not shown.
Binary file added images/menu1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/menu2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed images/pause.png
Binary file not shown.
Binary file added images/pause1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pause2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions inc/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _ECE_SPRITS_GAME_H_

#include <allegro.h>
#include <fblend.h>
#include <math.h>
#include "structures.h"
#include "objects.h"
Expand Down
19 changes: 15 additions & 4 deletions inc/structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,19 @@
#define UI_WIDTH 56
#define UI_PADDING_Y 5
#define UI_BUTTON_X 34
#define UI_BUTTON_Y 33
#define UI_BUTTON_Y 34
#define UI_FONT_SIZE 16
#define UI_SCROLL 20
#define UI_SCROLL_SPEED 5
#define UI_COUNTER_MAX 10

#define LEVEL_MINSTANDARD 1
#define LEVEL_MAXSTANDARD 3

#define LEVEL_SPEED_NONE 0
#define LEVEL_SPEED_SLOW 1
#define LEVEL_SPEED_FAST 2

typedef struct MenuItem MenuItem;
typedef struct CircularMenu CircularMenu;

Expand Down Expand Up @@ -214,9 +219,9 @@ struct Bitmaps {
BITMAP* branch;
BITMAP** stick_fire;
BITMAP* blow;
BITMAP* ui_accelerate;
BITMAP* ui_pause;
BITMAP* ui_menu;
Frames ui_accelerate;
Frames ui_pause;
Frames ui_menu;
Frames start;
Frames exit;
Frames stickmen_walking;
Expand All @@ -228,6 +233,7 @@ struct Bitmaps {
Frames stickmen_digging;
Frames stickmen_building;
Frames stickmen_blowing;
BITMAP* wave;
};

typedef struct FMod FMod;
Expand Down Expand Up @@ -257,6 +263,7 @@ struct Level {
char path[ 255 ];
Bitmaps bmps;
ObjectM* stickmen;
clock_t before;
float dt;
float slow_dt;
float fast_dt;
Expand All @@ -268,13 +275,17 @@ struct Level {
char start_direction;
char quit;
char win;
char speed;
float counter;
unsigned short nb_stickmen_max;
unsigned short nb_stickmen_added;
unsigned short nb_stickmen_arrived;
unsigned short nb_stickmen_should_arrive;
unsigned short nb_stickmen_dead;
unsigned short counter_stickmen_arrival;
unsigned short counter_stickmen_arrival_max;
unsigned short ui_counter;
unsigned short ui_frames;
CapacitiesNumber capacities;
CircularMenu* capacities_menu;
CircularMenu* directions_menu;
Expand Down
14 changes: 7 additions & 7 deletions level.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ char Level_loadImages( char* path, Level* level ) {
level->bmps.stick_fire = Level_loadBitmaps( "images/capacites/flamme", 3 );
level->bmps.blow = load_png( "images/capacites/souffle.png", NULL );

level->bmps.ui_accelerate = load_png( "images/acc.png", NULL );
level->bmps.ui_pause = load_png( "images/pause.png", NULL );
level->bmps.ui_menu = load_png( "images/menu.png", NULL );
level->bmps.ui_accelerate = Level_loadFrames( "images/acc", 2 );
level->bmps.ui_pause = Level_loadFrames( "images/pause", 2 );
level->bmps.ui_menu = Level_loadFrames( "images/menu", 2 );

if( !level->bmps.stickmen_walking.bmps || !level->bmps.stickmen_falling.bmps || !level->bmps.stickmen_dying.bmps || !level->bmps.stickmen_falldying.bmps || !level->bmps.stickmen_starting.bmps || !level->bmps.stickmen_exiting.bmps || !level->bmps.stickmen_digging.bmps || !level->bmps.stickmen_building.bmps || !level->bmps.stickmen_blowing.bmps || !level->bmps.start.bmps || !level->bmps.exit.bmps || !level->bmps.arrow || !level->bmps.capacity_build || !level->bmps.capacity_dig || !level->bmps.capacity_blow || !level->bmps.branch || !level->bmps.stick_fire || !level->bmps.blow || !level->bmps.ui_accelerate || !level->bmps.ui_pause || !level->bmps.ui_menu ) {
if( !level->bmps.stickmen_walking.bmps || !level->bmps.stickmen_falling.bmps || !level->bmps.stickmen_dying.bmps || !level->bmps.stickmen_falldying.bmps || !level->bmps.stickmen_starting.bmps || !level->bmps.stickmen_exiting.bmps || !level->bmps.stickmen_digging.bmps || !level->bmps.stickmen_building.bmps || !level->bmps.stickmen_blowing.bmps || !level->bmps.start.bmps || !level->bmps.exit.bmps || !level->bmps.arrow || !level->bmps.capacity_build || !level->bmps.capacity_dig || !level->bmps.capacity_blow || !level->bmps.branch || !level->bmps.stick_fire || !level->bmps.blow || !level->bmps.ui_accelerate.bmps || !level->bmps.ui_pause.bmps || !level->bmps.ui_menu.bmps ) {
allegro_message( "Impossible de charger les images" );
return 0;
}
Expand Down Expand Up @@ -248,9 +248,9 @@ void Level_free( Level* level ) {
Level_freeBitmap( level->bmps.branch );
Level_freeBitmaps( level->bmps.stick_fire, 3 );
Level_freeBitmap( level->bmps.blow );
Level_freeBitmap( level->bmps.ui_accelerate );
Level_freeBitmap( level->bmps.ui_pause );
Level_freeBitmap( level->bmps.ui_menu );
Level_freeFrames( &level->bmps.ui_accelerate );
Level_freeFrames( &level->bmps.ui_pause );
Level_freeFrames( &level->bmps.ui_menu );

if( level->bmps.droidsans_14_mono )
destroy_font( level->bmps.droidsans_14_mono );
Expand Down

0 comments on commit e7bdd7c

Please sign in to comment.