Added a mousepointer for later use.

This commit is contained in:
Linus Probert 2018-01-31 09:15:33 +01:00
parent c19fb67279
commit f3269436e0
9 changed files with 107 additions and 12 deletions

2
.vimrc
View File

@ -1,4 +1,6 @@
nnoremap <F1> :Make<cr> nnoremap <F1> :Make<cr>
nnoremap <F2> :Make clean<cr> nnoremap <F2> :Make clean<cr>
nnoremap <F3> :Make test<cr>
nnoremap <F4> :!./build/breakhack<cr>
let g:syntastic_c_include_dirs = [ 'build' ] let g:syntastic_c_include_dirs = [ 'build' ]

View File

@ -61,6 +61,7 @@ add_executable(breakhack
src/gui src/gui
src/item src/item
src/item_builder src/item_builder
src/pointer
) )
target_link_libraries(breakhack target_link_libraries(breakhack

View File

@ -9,3 +9,7 @@ clean:
test: test:
@make test -sC build @make test -sC build
.PHONY: test .PHONY: test
run: $(all)
@./build/breakhack
.PHONY: run

View File

@ -43,7 +43,8 @@ eat_flesh(Item *item, Player *player)
if (player->stats.hp > player->stats.maxhp) if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp; player->stats.hp = player->stats.maxhp;
gui_log("You eat some foul meat and gain %d health", player->stats.hp - original_hp); gui_log("You eat some foul meat and gain %d health",
player->stats.hp - original_hp);
} }
static void static void
@ -54,7 +55,8 @@ drink_health(Item *item, Player *player)
if (player->stats.hp > player->stats.maxhp) if (player->stats.hp > player->stats.maxhp)
player->stats.hp = player->stats.maxhp; player->stats.hp = player->stats.maxhp;
gui_log("You drink a health potion and gain %d health", player->stats.hp - original_hp); gui_log("You drink a health potion and gain %d health",
player->stats.hp - original_hp);
} }
static Item * static Item *
@ -82,11 +84,22 @@ pickup_gold(Item *item, Player *player)
} }
static Item * static Item *
create_treasure(void) create_treasure(int current_level)
{ {
unsigned int value = rand() % TREASURE_COUNT;
double amt = (unsigned int) rand() % 40; double amt = (unsigned int) rand() % 40;
char label[50]; char label[50];
unsigned int highest_treasure;
unsigned int value;
if (current_level > 15) {
highest_treasure = TREASURE_COUNT;
} else if (current_level > 5) {
highest_treasure = PLATINUM;
} else {
highest_treasure = GOLD;
}
value = rand() % highest_treasure;
SDL_Rect clip = { 0, 0, 16, 16 }; SDL_Rect clip = { 0, 0, 16, 16 };
switch (value) { switch (value) {
@ -125,7 +138,7 @@ create_treasure(void)
} }
Item * Item *
item_builder_build_item(ItemKey key) item_builder_build_item(ItemKey key, int level)
{ {
static const char *path_flesh = "assets/Items/Flesh.png"; static const char *path_flesh = "assets/Items/Flesh.png";
static const char *path_potion = "assets/Items/Potion.png"; static const char *path_potion = "assets/Items/Potion.png";
@ -135,7 +148,7 @@ item_builder_build_item(ItemKey key)
Item *item = NULL; Item *item = NULL;
switch (key) { switch (key) {
case TREASURE: case TREASURE:
item = create_treasure(); item = create_treasure(level);
break; break;
case FLESH: case FLESH:
item = create_item(path_flesh, item = create_item(path_flesh,

View File

@ -28,7 +28,7 @@ void
item_builder_init(SDL_Renderer *); item_builder_init(SDL_Renderer *);
Item * Item *
item_builder_build_item(ItemKey key); item_builder_build_item(ItemKey key, int level);
Item * Item *
item_builder_build_sack(void); item_builder_build_sack(void);

View File

@ -16,14 +16,15 @@
#include "gui.h" #include "gui.h"
#include "util.h" #include "util.h"
#include "item_builder.h" #include "item_builder.h"
#include "pointer.h"
static SDL_Window *gWindow = NULL; static SDL_Window *gWindow = NULL;
static SDL_Renderer *gRenderer = NULL; static SDL_Renderer *gRenderer = NULL;
static Player *gPlayer = NULL; static Player *gPlayer = NULL;
static LinkedList *gSpriteList = NULL;
static Map *gMap = NULL; static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL; static RoomMatrix *gRoomMatrix = NULL;
static Gui *gGui = NULL; static Gui *gGui = NULL;
static Pointer *gPointer = NULL;
static unsigned int cLevel = 1; static unsigned int cLevel = 1;
static double renderScale = 1.0; static double renderScale = 1.0;
static GameState gGameState; static GameState gGameState;
@ -114,10 +115,11 @@ initViewports(void)
static static
bool initGame(void) bool initGame(void)
{ {
gSpriteList = linkedlist_create();
initViewports(); initViewports();
gMap = map_lua_generator_run(cLevel, gRenderer); gMap = map_lua_generator_run(cLevel, gRenderer);
return gSpriteList == NULL; gPointer = pointer_create(gRenderer);
return true;
} }
static static
@ -161,6 +163,7 @@ bool handle_events(void)
camera_follow_position(&gCamera, &gPlayer->sprite->pos); camera_follow_position(&gCamera, &gPlayer->sprite->pos);
map_set_current_room(gMap, &gPlayer->sprite->pos); map_set_current_room(gMap, &gPlayer->sprite->pos);
} }
pointer_handle_event(gPointer, &event);
} }
return quit; return quit;
} }
@ -198,11 +201,13 @@ check_next_level(void)
static void static void
run_game(void) run_game(void)
{ {
SDL_RenderSetViewport(gRenderer, NULL);
map_clear_dead_monsters(gMap); map_clear_dead_monsters(gMap);
map_clear_collected_items(gMap); map_clear_collected_items(gMap);
roommatrix_populate_from_map(gRoomMatrix, gMap); roommatrix_populate_from_map(gRoomMatrix, gMap);
roommatrix_add_lightsource(gRoomMatrix, roommatrix_add_lightsource(gRoomMatrix,
&gPlayer->sprite->pos); &gPlayer->sprite->pos);
roommatrix_build_lightmap(gRoomMatrix); roommatrix_build_lightmap(gRoomMatrix);
gui_set_max_health(gGui, gPlayer->stats.maxhp, gRenderer); gui_set_max_health(gGui, gPlayer->stats.maxhp, gRenderer);
@ -228,6 +233,9 @@ run_game(void)
gui_render_log(gGui, BOTTOM_GUI_WIDTH, gui_render_log(gGui, BOTTOM_GUI_WIDTH,
BOTTOM_GUI_HEIGHT, &gCamera); BOTTOM_GUI_HEIGHT, &gCamera);
SDL_RenderSetViewport(gRenderer, NULL);
pointer_render(gPointer, &gCamera);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
if (check_if_dead()) if (check_if_dead())
@ -283,6 +291,7 @@ void close(void)
map_destroy(gMap); map_destroy(gMap);
roommatrix_destroy(gRoomMatrix); roommatrix_destroy(gRoomMatrix);
gui_destroy(gGui); gui_destroy(gGui);
pointer_destroy(gPointer);
item_builder_close(); item_builder_close();
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);

View File

@ -260,12 +260,12 @@ monster_drop_loot(Monster *monster, Map *map)
unsigned int item_count = 0; unsigned int item_count = 0;
if ((rand() % treasure_drop_chance) == 0) { if ((rand() % treasure_drop_chance) == 0) {
item = item_builder_build_item(TREASURE); item = item_builder_build_item(TREASURE, map->level);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
items[item_count++] = item; items[item_count++] = item;
} }
if ((rand() % item_drop_chance) == 0) { if ((rand() % item_drop_chance) == 0) {
item = item_builder_build_item(rand() % TREASURE); item = item_builder_build_item(rand() % TREASURE, map->level);
item->sprite->pos = monster->sprite->pos; item->sprite->pos = monster->sprite->pos;
items[item_count++] = item; items[item_count++] = item;
} }

42
src/pointer.c Normal file
View File

@ -0,0 +1,42 @@
#include "pointer.h"
#include "util.h"
Pointer *
pointer_create(SDL_Renderer *renderer)
{
SDL_ShowCursor(SDL_DISABLE);
Pointer *p = ec_malloc(sizeof(Pointer));
p->sprite = sprite_create();
sprite_load_texture(p->sprite, "assets/Items/MedWep.png", 0, renderer);
p->sprite->fixed = true;
p->sprite->clip = (SDL_Rect) { 0, 0, 16, 16 };
p->sprite->textures[0]->dim = (Dimension) {
TILE_DIMENSION, TILE_DIMENSION };
return p;
}
void
pointer_handle_event(Pointer *p, SDL_Event *event)
{
if (event->type == SDL_MOUSEMOTION) {
p->sprite->pos.x = event->motion.x;
p->sprite->pos.y = event->motion.y;
}
}
void
pointer_render(Pointer *p, Camera *cam)
{
sprite_render(p->sprite, cam);
}
void
pointer_destroy(Pointer *p)
{
SDL_ShowCursor(SDL_ENABLE);
sprite_destroy(p->sprite);
free(p);
}

24
src/pointer.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef POINTER_H_
#define POINTER_H_
#include <SDL2/SDL.h>
#include "sprite.h"
#include "camera.h"
typedef struct Pointer_t {
Sprite *sprite;
} Pointer;
Pointer *
pointer_create(SDL_Renderer *renderer);
void
pointer_handle_event(Pointer*, SDL_Event *event);
void
pointer_render(Pointer*, Camera*);
void
pointer_destroy(Pointer*);
#endif // POINTER_H_