commit 60496d7c9276675374a371a54bb8c217fe99aee4
parent 2b917eeda724888d271438f914ad93842c35d19a
Author: Samdal <samdal@protonmail.com>
Date: Wed, 28 Apr 2021 13:22:09 +0200
atoi to strol, board info, fix drawing
Diffstat:
M | Makefile | | | 2 | +- |
M | main.c | | | 66 | ++++++++++++++++++++++++++++++++++++++++++------------------------ |
2 files changed, 43 insertions(+), 25 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,7 +3,7 @@
#
# @file
# @version 0.1
-CFLAGS = -g -O -Wall -W -pedantic -std=c99
+CFLAGS = -g -O -Wall -W -pedantic -std=c99 -Wimplicit-fallthrough=0
LDLIBS = -lraylib -lm
all:
diff --git a/main.c b/main.c
@@ -1,14 +1,19 @@
-#include "raylib.h"
+#include <raylib.h>
#include <stdlib.h>
// Global variables
static bool shouldRedraw = false;
static int screenWidth = 800;
-static int screenHeight = 450;
+static int screenHeight = 800;
+static Vector2 squareSize;
static int TilesX = 20;
static int TilesY = 20;
-static int Bombs = 20;
-static Vector2 squareSize;
+static int TotalBombs = 20;
+typedef struct Bombs {
+ int x, y;
+} Bomb;
+static Bomb *bombPos; // all bombs position
+static bool *shown; // if a tile is hidden or not
// function declerations
static void InitGame(void);
@@ -21,46 +26,42 @@ static void DrawGame(void);
int main(int argc, char *argv[]) {
// Set game area and bomb amount with command line args
- //
- // TODO: bytt ut atoi med
- // https://stackoverflow.com/questions/2729460/why-do-i-get-this-unexpected-result-using-atoi-in-c
switch (argc) {
case 4:
- Bombs = atoi(argv[3]);
+ TotalBombs = strtol(argv[3], NULL, 0);
case 3:
- TilesY = atoi(argv[2]);
+ TilesY = strtol(argv[2], NULL, 0);
case 2:
- TilesX = atoi(argv[1]);
+ TilesX = strtol(argv[1], NULL, 0);
+ break;
}
+ // enable Vsync and resize window
SetConfigFlags(FLAG_WINDOW_RESIZABLE);
SetConfigFlags(FLAG_VSYNC_HINT);
-
InitWindow(screenWidth, screenHeight, "Minesweeper in C99");
InitGame();
+ if (bombPos == NULL || shown == NULL) // if memory allocation failed
+ return 255;
- while (!WindowShouldClose()) {
+ while (!WindowShouldClose())
UpdateDrawFrame();
- }
CloseWindow();
-
return 0;
}
void InitGame(void) {
-
// do not allow illegal values
- if (TilesX <= 0) {
+ if (TilesX <= 0)
TilesX = 20;
- }
- if (TilesY <= 0) {
+ if (TilesY <= 0)
TilesY = 20;
- }
- if (Bombs <= 0 || Bombs >= TilesX * TilesY) {
- Bombs = 20;
- }
+ if (TotalBombs <= 0 || TotalBombs >= TilesX * TilesY)
+ TotalBombs = 20;
+ bombPos = malloc(20 * sizeof(Bomb));
+ shown = calloc(TilesX * TilesY, sizeof(bool));
// square size
squareSize.x = (float)screenWidth / (float)TilesX;
@@ -70,11 +71,20 @@ void InitGame(void) {
shouldRedraw = true;
}
-void NewGame(void) { RedrawAllTiles(); }
+void NewGame(void) {
+ for (int i = 0; i < TotalBombs; i++) {
+ int randomx = rand() % TilesX;
+ int randomy = rand() % TilesY;
+ }
+ RedrawAllTiles();
+}
+
+void ExplodeAllBombs(void) {}
void RedrawAllTiles(void) {
ClearBackground(RAYWHITE);
shouldRedraw = false;
+ EndDrawing();
}
void UpdateDrawFrame(void) {
@@ -93,5 +103,13 @@ void UpdateDrawFrame(void) {
UpdateGame();
DrawGame();
}
+
void UpdateGame(void) {}
-void DrawGame(void) { EndDrawing(); }
+
+void DrawGame(void) {
+ if (shouldRedraw) {
+ RedrawAllTiles();
+ return;
+ }
+ EndDrawing();
+}