-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.c
162 lines (149 loc) · 5.3 KB
/
menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "menu.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "jsonParser.h"
Menu* createMenu(){
Menu *menu = malloc(sizeof (Menu));
menu->background = SDL_CreateRGBSurface(SDL_HWSURFACE, 150, 600, 24,0,0,0,0);
menu->blackTile = SDL_CreateRGBSurface(SDL_HWSURFACE, 32, 32, 24,0,0,0,0);
menu->currentItem = NULL;
menu->text = initTextArea();
menu->isUpdated = 1;
FILE *data;
data = fopen("resources/data.json", "r");
char* jsonFile = fileToString(data);
fclose(data);
TokenIterator *it = parseJson(jsonFile);
getNextStringValue(it,jsonFile,"towers");
for(int i=0; i<ItemNumbers; i++){
getNextStringValue(it,jsonFile,"location");
char* location = extractnToken(it->tokens[it->currentPosition], jsonFile, MaxDescriptionLenght);
getNextStringValue(it,jsonFile,"description");
char* description = extractnToken(it->tokens[it->currentPosition], jsonFile, MaxDescriptionLenght);
menu->items[i] = createMenuItem(location, description, i*32, 99);
menu->items[i]->values = malloc(sizeof (TowerValueLength)); //for item representing a tower
/* TowerValue value = Price;*/
GetNextStringValue("price");
menu->items[i]->values[Price] = atoi(ExtractToken(8));
/* value = Range;*/
GetNextStringValue("range");
menu->items[i]->values[Range] = atoi(ExtractToken(8));
getNextObject(it);
}
menu->coin = createAnimatedItem("resources/goldCoin.png", 10, 500); //!< \attention magic value
menu->playerMoney = 100;
drawMenu(menu);
return menu;
}
AnimatedItem* createAnimatedItem(const char* spriteSheet, int x, int y){
AnimatedItem *item = malloc(sizeof (AnimatedItem));
item->spriteSheet = IMG_Load(spriteSheet);
if(item->spriteSheet == NULL) {
printf("failed to load the animated item %s sprite\n", spriteSheet);
printf("IMG_Load: %s\n", IMG_GetError());
exit(-1);
}
item->animation[0] = NULL;
SDL_Rect sprite = {0,0,32,32}; //!< \attention magic number
for(int i=0; i<9; i++){
addEnemyAnimation((Enemy*)item,sprite,0);
sprite.x += sprite.w;
}
SDL_Rect position = {x,y,32,32}; //!< \attention magic number
item->position = position;
return item;
}
MenuItem* createMenuItem(const char* sprite, const char* description, int x, int y){
MenuItem *item = malloc(sizeof (MenuItem));
item->sprite = IMG_Load(sprite);
if(item->sprite == NULL) {
printf("failed to load the item %s sprite\n", sprite);
printf("IMG_Load: %s\n", IMG_GetError());
exit(-1);
}
item->description = description;
SDL_Rect position = {x,y,32,32}; //!< \attention magic number
item->position = position;
return item;
}
void updateMenu(Menu *menu){
menu->isUpdated = 1;
}
void clickOnMenu(Interface *interfaces, int x, int y){
Menu *menu = interfaces->menu;
for(int i=0; i<ItemNumbers; i++){
int xItem = menu->items[i]->position.x;
if(xItem <= x && x <= xItem+32){
int yItem = menu->items[i]->position.y;
if(yItem <= y && y <= yItem+32){
menu->currentItem = menu->items[i];
}
}
}
}
void drawMenu(Menu *menu){
for(int i=0; i<ItemNumbers; i++){
drawMenuItem(menu->items[i], menu->background);
}
if(menu->currentItem){
SDL_Rect currentItemPosition = {75,50,0,0};
SDL_Rect currentTextPosition = {5,50,0,0};
SDL_Rect spriteThumb = {0,0,32,32};
SDL_BlitSurface(menu->blackTile, &spriteThumb, menu->background, ¤tItemPosition);
SDL_BlitSurface(menu->currentItem->sprite, &spriteThumb, menu->background, ¤tItemPosition);
drawMenuText(menu->text, menu->currentItem);
SDL_BlitSurface(menu->text->sprite, &spriteThumb, menu->background, ¤tTextPosition);
}
drawAnimatedItem(menu, menu->coin);
}
void drawMenuItem(MenuItem *item, SDL_Surface *surfaceToDraw){
SDL_Rect spriteThumb = {0,0,32,32};
SDL_BlitSurface(item->sprite, &spriteThumb, surfaceToDraw, &item->position);
}
MenuText* initTextArea(){
SDL_Color white = {255,255,255,255};
TTF_Font *font_ = TTF_OpenFont("resources/zombieCat.ttf", 10);
if(!font_){
printf("font error: %s\n",TTF_GetError());
exit(-1);
}
/* TTF_Font *policeMini = TTF_OpenFont("resources/zombieCat.ttf", 14);*/
TTF_SetFontStyle(font_,TTF_STYLE_BOLD);
/* TTF_SetFontStyle(policeMini,TTF_STYLE_BOLD);*/
MenuText *text = malloc(sizeof (MenuText));
text->color = white;
text->font = font_;
text->sprite = SDL_CreateRGBSurface(SDL_HWSURFACE, 100, 32, 24,0,0,0,0);
return text;
}
void drawMenuText(MenuText *text, MenuItem *currentItem){
text->sprite = TTF_RenderUTF8_Solid(
text->font,
currentItem->description,
text->color
);
}
void drawAnimatedItem(Menu *menu, AnimatedItem *item){
SDL_Rect itemPosition = {item->position.x,item->position.y,item->position.w,item->position.h};
for(int i=0;i<8;i++){
SDL_BlitSurface(menu->blackTile, NULL, menu->background, &itemPosition);
itemPosition.x += itemPosition.w;
}
SDL_BlitSurface(item->spriteSheet, &item->animation[0]->animation, menu->background, &item->position);
item->animation[0] = item->animation[0]->nextAnimation;
char* currentMoney = malloc(8);
snprintf(currentMoney,8,"%d",menu->playerMoney);
SDL_Surface *money = TTF_RenderUTF8_Solid(
menu->text->font,
currentMoney,
menu->text->color
);
SDL_Rect textPosition = {item->position.x,item->position.y,item->position.w,item->position.h};
textPosition.x += textPosition.w + 9;
textPosition.y += textPosition.h/2;
SDL_BlitSurface(money, NULL, menu->background, &textPosition);
}