GRAVITYYY
This commit is contained in:
57
src/cells.c
57
src/cells.c
@@ -27,4 +27,59 @@
|
|||||||
* For more information, please refer to <https://unlicense.org/>
|
* For more information, please refer to <https://unlicense.org/>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "cells.h"
|
||||||
|
#include "raylib.h"
|
||||||
|
|
||||||
|
void drawGrid(int grid[CELL_AMOUNT][CELL_AMOUNT])
|
||||||
|
{
|
||||||
|
for (size_t i=0; i<CELL_AMOUNT; i++)
|
||||||
|
{
|
||||||
|
for (size_t j=0; j<CELL_AMOUNT; j++)
|
||||||
|
{
|
||||||
|
if (grid[i][j] == VOID)
|
||||||
|
{
|
||||||
|
DrawRectangle(CELL_SIZE_PIXELS*j, CELL_SIZE_PIXELS*i, CELL_SIZE_PIXELS, CELL_SIZE_PIXELS, BLACK);
|
||||||
|
}
|
||||||
|
if (grid[i][j] == SAND)
|
||||||
|
{
|
||||||
|
DrawRectangle(CELL_SIZE_PIXELS*j, CELL_SIZE_PIXELS*i, CELL_SIZE_PIXELS, CELL_SIZE_PIXELS, MAROON);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void putCell(int grid[CELL_AMOUNT][CELL_AMOUNT], int posX, int posY)
|
||||||
|
{
|
||||||
|
grid[posX][posY] = SAND;
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateGrid(int grid[CELL_AMOUNT][CELL_AMOUNT])
|
||||||
|
{
|
||||||
|
for (int i=CELL_AMOUNT-1; i>=0; i--)
|
||||||
|
{
|
||||||
|
for (int j=0; j<CELL_AMOUNT; j++)
|
||||||
|
{
|
||||||
|
switch (grid[i][j])
|
||||||
|
{
|
||||||
|
case VOID:
|
||||||
|
break;
|
||||||
|
case SAND:
|
||||||
|
if (i+1 < CELL_AMOUNT && grid[i+1][j] == VOID) {
|
||||||
|
grid[i][j] = VOID;
|
||||||
|
grid[i+1][j] = SAND; // falling downwards
|
||||||
|
} else if (i+1 < CELL_AMOUNT && j+1 < CELL_AMOUNT && grid[i+1][j+1] == VOID) {
|
||||||
|
grid[i][j] = VOID;
|
||||||
|
grid[i+1][j+1] = SAND; // falling down-right
|
||||||
|
}
|
||||||
|
else if (i+1 < CELL_AMOUNT && j-1 >= 0 && grid[i+1][j-1] == VOID) {
|
||||||
|
grid[i][j] = VOID;
|
||||||
|
grid[i+1][j-1] = SAND; // falling down-left
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/cells.h
22
src/cells.h
@@ -30,4 +30,26 @@
|
|||||||
#ifndef CELLS_H
|
#ifndef CELLS_H
|
||||||
#define CELLS_H
|
#define CELLS_H
|
||||||
|
|
||||||
|
#define CELL_AMOUNT 80
|
||||||
|
#define CELL_SIZE_PIXELS 10
|
||||||
|
#define TARGET_FPS 60
|
||||||
|
|
||||||
|
#define WINDOW_SIZE_PIXELS (CELL_AMOUNT * CELL_SIZE_PIXELS)
|
||||||
|
|
||||||
|
typedef enum GameScreen
|
||||||
|
{
|
||||||
|
TITLE,
|
||||||
|
GAME
|
||||||
|
} GameScreen;
|
||||||
|
|
||||||
|
typedef enum CellType
|
||||||
|
{
|
||||||
|
VOID,
|
||||||
|
SAND
|
||||||
|
} CellType;
|
||||||
|
|
||||||
|
void drawGrid(int grid[CELL_AMOUNT][CELL_AMOUNT]);
|
||||||
|
void putCell(int grid[CELL_AMOUNT][CELL_AMOUNT], int posX, int posY);
|
||||||
|
void updateGrid(int grid[CELL_AMOUNT][CELL_AMOUNT]);
|
||||||
|
|
||||||
#endif // CELLS_H
|
#endif // CELLS_H
|
||||||
52
src/main.c
52
src/main.c
@@ -32,18 +32,58 @@
|
|||||||
|
|
||||||
int main ()
|
int main ()
|
||||||
{
|
{
|
||||||
|
int grid[CELL_AMOUNT][CELL_AMOUNT] = {0};
|
||||||
|
GameScreen currentScreen = TITLE;
|
||||||
|
|
||||||
SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
|
SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_HIGHDPI);
|
||||||
InitWindow(1280, 800, "Sandbox");
|
InitWindow(WINDOW_SIZE_PIXELS, WINDOW_SIZE_PIXELS, "Sandbox");
|
||||||
|
SetTargetFPS(TARGET_FPS);
|
||||||
|
|
||||||
// game loop
|
// game loop
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
test();
|
/* Update */
|
||||||
// drawing
|
switch (currentScreen)
|
||||||
BeginDrawing();
|
{
|
||||||
|
case TITLE:
|
||||||
|
if (IsKeyPressed(KEY_SPACE)) {
|
||||||
|
currentScreen = GAME;
|
||||||
|
ClearBackground(BLACK);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case GAME:
|
||||||
|
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||||
|
Vector2 cellPosition = GetMousePosition();
|
||||||
|
int i = cellPosition.y/CELL_SIZE_PIXELS;
|
||||||
|
int j = cellPosition.x/CELL_SIZE_PIXELS;
|
||||||
|
putCell(grid, i, j);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Setup the back buffer for drawing (clear color and depth buffers)
|
/* Drawing */
|
||||||
ClearBackground(BLACK);
|
BeginDrawing();
|
||||||
|
switch (currentScreen)
|
||||||
|
{
|
||||||
|
case TITLE:
|
||||||
|
int titleWidth = MeasureText("SANDBOX", 40);
|
||||||
|
DrawRectangle(0, 0, WINDOW_SIZE_PIXELS, WINDOW_SIZE_PIXELS, DARKBLUE);
|
||||||
|
DrawText("SANDBOX", (WINDOW_SIZE_PIXELS-titleWidth)/2, 20, 40, WHITE);
|
||||||
|
int controlsWidth = MeasureText("Controls", 30);
|
||||||
|
DrawText("Controls", (WINDOW_SIZE_PIXELS-controlsWidth)/2, 100, 30, WHITE);
|
||||||
|
DrawText("Left click to spawn sand", 25, 150, 30, WHITE);
|
||||||
|
int startGameWidth = MeasureText("Press Space to start!", 30);
|
||||||
|
DrawText("Press Space to start!", (WINDOW_SIZE_PIXELS-startGameWidth)/2, 400, 30, WHITE);
|
||||||
|
break;
|
||||||
|
case GAME:
|
||||||
|
updateGrid(grid);
|
||||||
|
drawGrid(grid);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// end the frame and get ready for the next one (display frame, poll input, etc...)
|
// end the frame and get ready for the next one (display frame, poll input, etc...)
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
|
|||||||
Reference in New Issue
Block a user