Skip to content

Commit 4595256

Browse files
authored
Web build fixes (#1)
* oops * using args * passing pointer * whoops * no returns * other fixes * attempting void pointers
1 parent c1ab3e1 commit 4595256

4 files changed

Lines changed: 39 additions & 20 deletions

File tree

src/button.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@
33
Button InitButton(Texture2D text, Vector2 offset, Sound soundFx, GameState state)
44
{
55
// Define frame rectangle for drawing
6-
frameHeight = (float)text.height / BUTTON_FRAMES;
7-
Rectangle sourceRec = {0, 0, (float)text.width, frameHeight};
6+
float height = (float)text.height / BUTTON_FRAMES;
7+
Rectangle sourceRec = {0, 0, (float)text.width, height};
88

99
// Define button position on screen
1010
Rectangle btnPosition = {
1111
screenWidth / 2.0f - text.width / 2.0f + offset.x,
1212
screenHeight / 2.0f - text.height / BUTTON_FRAMES / 2.0f + offset.y,
1313
(float)text.width,
14-
frameHeight};
14+
height};
1515

1616
return (Button){
1717
.texture = text,
1818
.rect = sourceRec,
1919
.position = btnPosition,
20+
.frameHeight = height,
2021
.fx = soundFx,
2122
.selected = false,
2223
.selectedState = state,
@@ -57,7 +58,7 @@ void UpdateButton(Button *button)
5758
}
5859

5960
// Calculate button frame rectangle to draw depending on button state
60-
button->rect.y = btnState * frameHeight;
61+
button->rect.y = btnState * button->frameHeight;
6162
}
6263

6364
void DrawButton(Button *button)

src/button.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ typedef struct Button
1313
Texture2D texture;
1414
Rectangle rect;
1515
Rectangle position;
16+
float frameHeight;
1617
Sound fx;
1718
bool selected;
1819
GameState selectedState;
1920
} Button;
2021

21-
static float frameHeight;
22-
2322
Button InitButton(Texture2D text, Vector2 offset, Sound soundFx, GameState state);
2423
void UpdateButton(Button *button);
2524
void DrawButton(Button *button);

src/raylib_game

-4 KB
Binary file not shown.

src/raylib_game.c

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void ScoreState(ScoreMenu *menu);
8080
static void PlayState(Camera3D camera, Clock *clock, Snake *snake, Food *food);
8181
// static void IntroState(Camera3D camera, Clock *clock, Snake *snake, Food *food);
8282
// static void OutroState(Camera3D camera, Clock *clock, Snake *snake, Food *food);
83-
static bool UpdateDrawFrame(GameData data);
83+
static void UpdateDrawFrame(void *dataPtr);
8484

8585
//------------------------------------------------------------------------------------
8686
// Program main entry point
@@ -137,7 +137,15 @@ int main(void)
137137
// movement clock
138138
Clock clock = InitClock(clockStartRate);
139139

140-
GameData gameData = (GameData){
140+
void *dataPtr;
141+
dataPtr = malloc(sizeof(GameData));
142+
if (dataPtr == NULL)
143+
{
144+
printf("memory allocation of data pointer failed!\n");
145+
return 1;
146+
}
147+
148+
dataPtr = &(GameData){
141149
.camera = camera,
142150
.clock = &clock,
143151
.snake = &snake,
@@ -147,13 +155,23 @@ int main(void)
147155
.score = &scoreMenu,
148156
};
149157

158+
// GameData *gameData = &(GameData){
159+
// .camera = camera,
160+
// .clock = &clock,
161+
// .snake = &snake,
162+
// .food = &food,
163+
// .main = &mainMenu,
164+
// .credits = &creditsMenu,
165+
// .score = &scoreMenu,
166+
// };
167+
150168
// Render texture to draw full screen, enables screen scaling
151169
// NOTE: If screen is scaled, mouse input should be scaled proportionally
152170
target = LoadRenderTexture(screenWidth, screenHeight);
153171
SetTextureFilter(target.texture, TEXTURE_FILTER_BILINEAR);
154172

155173
#if defined(PLATFORM_WEB)
156-
emscripten_set_main_loopbool UpdateDrawFrame, 60, 1);
174+
emscripten_set_main_loop_arg(UpdateDrawFrame, dataPtr, 60, 1);
157175
#else
158176
SetTargetFPS(60); // Set our game frames-per-second
159177
//--------------------------------------------------------------------------------------
@@ -162,7 +180,7 @@ int main(void)
162180
bool exitWindow = false;
163181
while (!exitWindow)
164182
{
165-
UpdateDrawFrame(gameData);
183+
UpdateDrawFrame(dataPtr);
166184

167185
if (activeState == GAME_EXIT || WindowShouldClose())
168186
exitWindow = true;
@@ -191,25 +209,28 @@ int main(void)
191209
// Module functions definition
192210
//--------------------------------------------------------------------------------------------
193211
// Update and draw frame
194-
bool UpdateDrawFrame(GameData data)
212+
void UpdateDrawFrame(void *dataPtr)
195213
{
214+
GameData *data = {0};
215+
data = (GameData *)data;
216+
196217
switch (activeState)
197218
{
198219
case GAME_MENU:
199220
{
200-
MenuState(data.main);
221+
MenuState(data->main);
201222

202223
// setup game
203224
if (activeState == GAME_PLAY)
204225
{
205-
SetupGame(&data);
226+
SetupGame(data);
206227
}
207228

208229
break;
209230
}
210231
case GAME_CREDITS:
211232
{
212-
CreditsState(data.credits);
233+
CreditsState(data->credits);
213234
break;
214235
}
215236
case GAME_INTRO:
@@ -219,17 +240,17 @@ bool UpdateDrawFrame(GameData data)
219240
}
220241
case GAME_PLAY:
221242
{
222-
PlayState(data.camera, data.clock, data.snake, data.food);
243+
PlayState(data->camera, data->clock, data->snake, data->food);
223244
break;
224245
}
225246
case GAME_SCORE:
226247
{
227-
ScoreState(data.score);
248+
ScoreState(data->score);
228249

229250
// setup game
230251
if (activeState == GAME_PLAY)
231252
{
232-
SetupGame(&data);
253+
SetupGame(data);
233254
}
234255

235256
break;
@@ -241,15 +262,13 @@ bool UpdateDrawFrame(GameData data)
241262
}
242263
case GAME_EXIT:
243264
{
244-
return true;
265+
activeState = GAME_EXIT;
245266
}
246267
case GAME_NONE:
247268
{
248269
break;
249270
}
250271
}
251-
252-
return false;
253272
}
254273

255274
void SetupGame(GameData *data)

0 commit comments

Comments
 (0)