From 389c4c27aa58c1c4dff26aca6ae61bd538d6fd7a Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Wed, 9 May 2012 10:58:45 +0200 Subject: [PATCH] add "power meter" --- Makefile | 4 ++-- src/allegro_utils.c | 24 ++++++++++++++++++++++++ src/allegro_utils.h | 7 +++++++ src/level.c | 25 ++++++++++++++++++++++++- src/main.c | 10 +++++----- src/main.h | 3 +++ 6 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/allegro_utils.c create mode 100644 src/allegro_utils.h diff --git a/Makefile b/Makefile index 442e527..1a9fa92 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,9 @@ CFLAGS=-I$(IDIR) -Wall -ansi -g SRCDIR=src ODIR=obj -LIBS=-lallegro -lallegro_audio -lallegro_acodec -lallegro_image -lallegro_font -lallegro_main -lallegro_ttf -lm +LIBS=-lallegro -lallegro_audio -lallegro_acodec -lallegro_image -lallegro_font -lallegro_main -lallegro_ttf -lallegro_primitives -lm -_OBJ = config.o main.o about.o intro.o loading.o map.o menu.o level.o moonwalk.o pause.o timeline.o +_OBJ = config.o main.o about.o intro.o loading.o map.o menu.o level.o moonwalk.o pause.o timeline.o allegro_utils.o OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ)) OUTPUTDIR = bin diff --git a/src/allegro_utils.c b/src/allegro_utils.c new file mode 100644 index 0000000..9e7640a --- /dev/null +++ b/src/allegro_utils.c @@ -0,0 +1,24 @@ +#include "allegro_utils.h" + +void al_draw_vertical_gradient_rect(float x, float y, float w, float h, ALLEGRO_COLOR top, ALLEGRO_COLOR bottom) { + ALLEGRO_VERTEX v[] = { + {.x = x, .y = y, .z = 0, .color = top}, + {.x = x + w, .y = y, .z = 0, .color = top}, + {.x = x, .y = y + h, .z = 0, .color = bottom}, + {.x = x + w, .y = y + h, .z = 0, .color = bottom}}; + al_draw_prim(v, NULL, NULL, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP); +} + +void al_draw_horizontal_gradient_rect(float x, float y, float w, float h, ALLEGRO_COLOR left, ALLEGRO_COLOR right) { + ALLEGRO_VERTEX v[] = { + {.x = x, .y = y, .z = 0, .color = left}, + {.x = x + w, .y = y, .z = 0, .color = right}, + {.x = x, .y = y + h, .z = 0, .color = left}, + {.x = x + w, .y = y + h, .z = 0, .color = right}}; + al_draw_prim(v, NULL, NULL, 0, 4, ALLEGRO_PRIM_TRIANGLE_STRIP); +} + +void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text) { + al_draw_text(font, al_map_rgba(0,0,0,128), x+1, y+1, flags, text); + al_draw_text(font, color, x, y, flags, text); +} diff --git a/src/allegro_utils.h b/src/allegro_utils.h new file mode 100644 index 0000000..f131c99 --- /dev/null +++ b/src/allegro_utils.h @@ -0,0 +1,7 @@ +#include +#include +#include + +void al_draw_vertical_gradient_rect(float x, float y, float w, float h, ALLEGRO_COLOR top, ALLEGRO_COLOR bottom); +void al_draw_horizontal_gradient_rect(float x, float y, float w, float h, ALLEGRO_COLOR left, ALLEGRO_COLOR right); +void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text); \ No newline at end of file diff --git a/src/level.c b/src/level.c index 9299a67..a1a1136 100644 --- a/src/level.c +++ b/src/level.c @@ -113,10 +113,24 @@ bool Move(struct Game *game, struct TM_Action *action, enum TM_ActionState state return true; } +bool ShowMeter(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { + if (state != TM_ACTIONSTATE_RUNNING) return false; + game->level.meter_alpha+=tps(game, 60*3); + if (game->level.meter_alpha>=255) { + game->level.meter_alpha=255; + return true; + } + return false; +} + bool Fly(struct Game *game, struct TM_Action *action, enum TM_ActionState state) { if (state == TM_ACTIONSTATE_INIT) action->arguments = NULL; if (state != TM_ACTIONSTATE_RUNNING) return false; - if (!(action->arguments)) { SelectDerpySpritesheet(game, "fly"); game->level.flying = true; } + if (!(action->arguments)) { + SelectDerpySpritesheet(game, "fly"); + game->level.flying = true; + TM_AddBackgroundAction(&ShowMeter, NULL, 0, "showmeter"); + } action->arguments++; game->level.derpy_y-=tps(game, 60*0.004); if (game->level.derpy_y>0.2) return false; @@ -312,6 +326,11 @@ void Level_Draw(struct Game *game) { if (game->level.cl_pos >= 1) game->level.cl_pos=game->level.cl_pos-1; al_hold_bitmap_drawing(false); + al_set_target_bitmap(game->level.meter_bmp); + al_draw_horizontal_gradient_rect(0, 0, al_get_bitmap_width(game->level.meter_bmp), al_get_bitmap_height(game->level.meter_bmp), al_map_rgb(255,0,0), al_map_rgb(0,255,0)); + al_set_target_bitmap(al_get_backbuffer(game->display)); + al_draw_tinted_bitmap(game->level.meter_bmp, al_map_rgba(game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha,game->level.meter_alpha), al_get_display_width(game->display)*0.75, al_get_display_height(game->display)*0.925, 0); + TM_Process(); } } @@ -430,6 +449,7 @@ void Level_Load(struct Game *game) { game->level.handle_input = false; game->level.obstracles = NULL; game->level.flying = false; + game->level.meter_alpha=0; al_clear_to_color(al_map_rgb(0,0,0)); if (game->level.current_level!=1) Moonwalk_Load(game); else { @@ -555,6 +575,7 @@ void Level_UnloadBitmaps(struct Game *game) { al_destroy_bitmap(game->level.clouds); al_destroy_bitmap(game->level.welcome); al_destroy_bitmap(game->level.obst_bmps.pie); + al_destroy_bitmap(game->level.meter_bmp); } } @@ -582,5 +603,7 @@ void Level_PreloadBitmaps(struct Game *game) { al_draw_text_with_shadow(game->menu.font_title, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.1, ALLEGRO_ALIGN_CENTRE, "Level 1"); al_draw_text_with_shadow(game->menu.font_subtitle, al_map_rgb(255,255,255), al_get_display_width(game->display)*0.5, al_get_display_height(game->display)*0.275, ALLEGRO_ALIGN_CENTRE, "Fluttershy"); al_set_target_bitmap(al_get_backbuffer(game->display)); + + game->level.meter_bmp = al_create_bitmap(al_get_display_width(game->display)*0.2, al_get_display_height(game->display)*0.025); } } diff --git a/src/main.c b/src/main.c index 0d7304a..dda5a41 100644 --- a/src/main.c +++ b/src/main.c @@ -60,11 +60,6 @@ double old_time = 0, fps; int frames_done = 0; -void al_draw_text_with_shadow(ALLEGRO_FONT *font, ALLEGRO_COLOR color, float x, float y, int flags, char const *text) { - al_draw_text(font, al_map_rgba(0,0,0,128), x+1, y+1, flags, text); - al_draw_text(font, color, x, y, flags, text); -} - void PrintConsole(struct Game *game, char* format, ...) { va_list vl; va_start(vl, format); @@ -329,6 +324,11 @@ int main(int argc, char **argv){ return -1; } + if(!al_init_primitives_addon()){ + fprintf(stderr, "failed to initialize primitives!\n"); + return -1; + } + /* if (!al_reserve_samples(10)){ fprintf(stderr, "failed to reserve samples!\n"); return -1; diff --git a/src/main.h b/src/main.h index a028157..5e489ac 100644 --- a/src/main.h +++ b/src/main.h @@ -29,6 +29,7 @@ #include #include #include +#include "allegro_utils.h" struct Game; @@ -77,6 +78,7 @@ struct Level { float derpy_x, derpy_y; bool handle_input; bool flying; + float meter_alpha; int sheet_rows, sheet_cols, sheet_pos, sheet_blanks; float sheet_tmp, sheet_speed, sheet_speed_modifier; ALLEGRO_SAMPLE *sample; /*!< Sample with background music. */ @@ -86,6 +88,7 @@ struct Level { ALLEGRO_BITMAP *welcome; ALLEGRO_BITMAP **derpy_sheet; /*!< Active Derpy sprite sheet. */ ALLEGRO_BITMAP *derpy; /*!< Derpy sprite. */ + ALLEGRO_BITMAP *meter_bmp; struct Spritesheet* derpy_sheets; /*!< List of sprite sheets of Derpy character. */ struct Spritesheet* pony_sheets; /*!< List of sprite sheets of character rescued by Derpy. */ struct {