Began work on C version
This commit is contained in:
1 parent
434cc918a4
commit
8dc9acb85f
3 files changed
+117
-10
No files matched your search
+105
-4
@@ -1,6 +1,107 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
void main(int argc, char *argv[]){
|
#include <SDL3/SDL.h>
|
||||||
printf("Hello World!\n");
|
#include <SDL3/SDL_main.h>
|
||||||
|
|
||||||
}
|
static SDL_Window *window = NULL;
|
||||||
|
static SDL_Renderer *renderer = NULL;
|
||||||
|
static Uint64 last_time = 0;
|
||||||
|
|
||||||
|
#define WINDOW_HEIGHT = 1080;
|
||||||
|
#define WINDOW_WIDTH = 1920;
|
||||||
|
|
||||||
|
#define MIN_PIXELS_PER_SECOND = 10;
|
||||||
|
#define MAX_PIXELS_PER_SECOND = 20;
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
// Constants
|
||||||
|
int GAME_X, GAME_Y;
|
||||||
|
GAME_X = 1920;
|
||||||
|
GAME_Y = 1080;
|
||||||
|
bool RUNNING = true;
|
||||||
|
// Use Calloc to ensure the start is all 0's'
|
||||||
|
unsigned char *GAME_MATRIX =
|
||||||
|
(unsigned char *)calloc(GAME_X * GAME_Y, sizeof(bool));
|
||||||
|
|
||||||
|
//-----------------------------------------------------
|
||||||
|
// Var declarations
|
||||||
|
int scale, mouse_x, mouse_y;
|
||||||
|
scale = 10;
|
||||||
|
|
||||||
|
//-----------------------------------------------------
|
||||||
|
// Init
|
||||||
|
if (GAME_MATRIX == NULL) {
|
||||||
|
printf("\nCalloc failed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (!SDL_INIT(SDL_INIT_VIDEO)) {
|
||||||
|
printf("\nCould not initiallize SDL_Window");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------
|
||||||
|
// Logic
|
||||||
|
while (RUNNING) {
|
||||||
|
for (int y = 0; y < GAME_Y; y++) {
|
||||||
|
for (int x = 0; x < GAME_X; x++) {
|
||||||
|
// adjust for indexing
|
||||||
|
unsigned int index = y * GAME_X + x;
|
||||||
|
if (GAME_MATRIX[index]) {
|
||||||
|
int live_count = 0;
|
||||||
|
// Get neighbor indexes
|
||||||
|
unsigned int north, south, east, west, north_east, north_west,
|
||||||
|
south_east, south_west;
|
||||||
|
north = index - GAME_X;
|
||||||
|
north_east = index - GAME_X + 1;
|
||||||
|
east = index + 1;
|
||||||
|
south_west = index + GAME_X + 1;
|
||||||
|
south = index + GAME_X;
|
||||||
|
south_east = index + GAME_X - 1;
|
||||||
|
west = index - 1;
|
||||||
|
north_west = index - GAME_X - 1;
|
||||||
|
// Error check for boundary issue
|
||||||
|
// Need to find a way to have no neighbor rather than set the index to
|
||||||
|
// 0
|
||||||
|
if (north < 0) {
|
||||||
|
north = 0;
|
||||||
|
}
|
||||||
|
if (north_east < 0) {
|
||||||
|
north_east = 0;
|
||||||
|
}
|
||||||
|
if (north_west < 0) {
|
||||||
|
north_west = 0;
|
||||||
|
}
|
||||||
|
if (south > GAME_Y) {
|
||||||
|
south = 0;
|
||||||
|
}
|
||||||
|
if (south_east > GAME_Y) {
|
||||||
|
south_east = 0;
|
||||||
|
}
|
||||||
|
if (south_west > GAME_Y) {
|
||||||
|
south_west = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust count (value will either be a 1 or a 0)
|
||||||
|
if (GAME_MATRIX[north] | GAME_MATRIX[north_west] | GAME_MATRIX[west] |
|
||||||
|
GAME_MATRIX[south_west] | GAME_MATRIX[south] | GAME_MATRIX[east] |
|
||||||
|
GAME_MATRIX[south_east] | GAME_MATRIX[north_east]) {
|
||||||
|
live_count += 1;
|
||||||
|
}
|
||||||
|
// Check if cell should live or die
|
||||||
|
if (live_count >= 4) {
|
||||||
|
GAME_MATRIX[index] = true;
|
||||||
|
} else {
|
||||||
|
GAME_MATRIX[index] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//-----------------------------------------------------
|
||||||
|
// Close out the game
|
||||||
|
free(GAME_MATRIX);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
void SDL_AppQuit(void *appstate, SDL_AppResult result) {}
|
||||||
Binary file not shown.
+12
-6
@@ -1,6 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
import pygame
|
import pygame
|
||||||
import os
|
|
||||||
import pygame_menu
|
import pygame_menu
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import math
|
import math
|
||||||
@@ -44,9 +43,9 @@ def updateGame():
|
|||||||
for x in range(GRID_W):
|
for x in range(GRID_W):
|
||||||
for y in range(GRID_H):
|
for y in range(GRID_H):
|
||||||
color = ()
|
color = ()
|
||||||
if GRID[y, x] == True:
|
if GRID[y, x]:
|
||||||
color = (255, 255, 255)
|
color = (255, 255, 255)
|
||||||
if GRID[y, x] == False:
|
if not GRID[y, x]:
|
||||||
color = (0, 0, 0)
|
color = (0, 0, 0)
|
||||||
drawRect(x * CELL_SIZE, y * CELL_SIZE, color)
|
drawRect(x * CELL_SIZE, y * CELL_SIZE, color)
|
||||||
|
|
||||||
@@ -259,10 +258,11 @@ def main():
|
|||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
importlib.import_module("pygame_menu")
|
importlib.import_module("pygame_menu")
|
||||||
|
|
||||||
# Initialization
|
# Initialization
|
||||||
pygame.init()
|
pygame.init()
|
||||||
try:
|
try:
|
||||||
# initialize the screen and set the height and widths
|
# Initialize the screen and set the height and widths
|
||||||
SCREEN = pygame.display.set_mode(
|
SCREEN = pygame.display.set_mode(
|
||||||
(SCREEN_X, SCREEN_Y), pygame.RESIZABLE | pygame.SCALED
|
(SCREEN_X, SCREEN_Y), pygame.RESIZABLE | pygame.SCALED
|
||||||
)
|
)
|
||||||
@@ -271,8 +271,13 @@ def main():
|
|||||||
window_w, window_h = pygame.display.get_window_size()
|
window_w, window_h = pygame.display.get_window_size()
|
||||||
|
|
||||||
if SCREEN:
|
if SCREEN:
|
||||||
SCALE_X = window_w / SCREEN_X | 1
|
SCALE_X = window_w / SCREEN_X
|
||||||
SCALE_Y = window_h / SCREEN_Y | 1
|
SCALE_Y = window_h / SCREEN_Y
|
||||||
|
# Normalize the scale
|
||||||
|
if SCALE_X > 1:
|
||||||
|
SCALE_X = 1
|
||||||
|
if SCALE_Y > 1:
|
||||||
|
SCALE_Y = 1
|
||||||
|
|
||||||
MENU_Y = 0.9 * SCREEN_Y * SCALE_Y
|
MENU_Y = 0.9 * SCREEN_Y * SCALE_Y
|
||||||
MENU_X = 0.8 * SCREEN_X * SCALE_X
|
MENU_X = 0.8 * SCREEN_X * SCALE_X
|
||||||
@@ -287,6 +292,7 @@ def main():
|
|||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
# Update screen
|
# Update screen
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|||||||
Reference in new issue
Block a user