174 lines
4.0 KiB
C
174 lines
4.0 KiB
C
#include "SDL2/SDL.h"
|
|
#include <SDL2/SDL_events.h>
|
|
#include <SDL2/SDL_hints.h>
|
|
#include <SDL2/SDL_keycode.h>
|
|
#include <SDL2/SDL_mouse.h>
|
|
#include <SDL2/SDL_rect.h>
|
|
#include <SDL2/SDL_render.h>
|
|
#include <SDL2/SDL_thread.h>
|
|
#include <SDL2/SDL_timer.h>
|
|
#include <SDL2/SDL_video.h>
|
|
#include <SDL2/SDL_image.h>
|
|
#include <SDL2/SDL_ttf.h>
|
|
#include <stdlib.h>
|
|
#include "cell.h"
|
|
|
|
const int SCREEN_WIDTH = 1280;
|
|
const int SCREEN_HEIGHT = 720;
|
|
const int CELL_SIZE = 15;
|
|
const int MAX_X = SCREEN_WIDTH / CELL_SIZE - 1;
|
|
const int MAX_Y = SCREEN_HEIGHT / CELL_SIZE - 1;
|
|
|
|
typedef struct {
|
|
SDL_Window *window;
|
|
SDL_Renderer *renderer;
|
|
TTF_Font* font;
|
|
} App;
|
|
|
|
typedef struct {
|
|
Cell** cells;
|
|
int size;
|
|
unsigned int capcity;
|
|
} CellList;
|
|
|
|
CellList* GlobalList;
|
|
|
|
App app;
|
|
|
|
void Cleanup(void);
|
|
void HandleInput(void);
|
|
int GetIndex(int x, int y, int columns);
|
|
int GetNotSoRandomNumber(int);
|
|
|
|
int main(int argc, char** argv) {
|
|
memset(&app, 0, sizeof(app));
|
|
|
|
atexit(Cleanup);
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0){
|
|
printf("SDL failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
if (TTF_Init() < 0) {
|
|
printf("Failed to init TTF: %s\n", TTF_GetError());
|
|
return -1;
|
|
}
|
|
|
|
app.font = TTF_OpenFont("/usr/share/fonts/TTF/Hack-Regular.ttf", 14);
|
|
|
|
if (app.font == NULL) {
|
|
printf("Failed to load font\n");
|
|
return -1;
|
|
}
|
|
|
|
app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
|
|
|
|
if (app.window == NULL){
|
|
printf("Window creation failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
|
|
|
|
app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED);
|
|
|
|
if (app.renderer == NULL) {
|
|
printf("Renderer failed: %s\n", SDL_GetError());
|
|
return -1;
|
|
}
|
|
|
|
GlobalList = calloc(1, sizeof(CellList));
|
|
GlobalList->cells = calloc(GetIndex(MAX_X, MAX_Y, MAX_X), sizeof(Cell*));
|
|
GlobalList->capcity = GetIndex(MAX_X, MAX_Y, MAX_X);
|
|
GlobalList->size = 0;
|
|
|
|
SDL_Rect* rect = malloc(sizeof(SDL_Rect));
|
|
rect->h = CELL_SIZE;
|
|
rect->w = CELL_SIZE;
|
|
// rect->x = 0;//MAX_X * CELL_SIZE;
|
|
// rect->y = 0;//MAX_Y * CELL_SIZE;
|
|
// Cell* cell = CreateCell(*rect);
|
|
|
|
while(1){
|
|
|
|
HandleInput();
|
|
|
|
SDL_Delay(128);
|
|
|
|
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(app.renderer);
|
|
|
|
SDL_SetRenderDrawColor(app.renderer, 255, 255, 255, 255);
|
|
|
|
for(int i = 0; i < GlobalList->capcity; i++) {
|
|
if (GlobalList->cells[i] == NULL) continue;
|
|
|
|
Cell* cell = GlobalList->cells[i];
|
|
|
|
rect->x = cell->x * CELL_SIZE;
|
|
rect->y = cell->y * CELL_SIZE;
|
|
|
|
SDL_RenderFillRect(app.renderer, rect);
|
|
}
|
|
|
|
// rect->x += CELL_SIZE;
|
|
// rect->y += CELL_SIZE;
|
|
|
|
// if (rect->x / CELL_SIZE > MAX_X) rect->x = 0;
|
|
// if (rect->y / CELL_SIZE > MAX_Y) rect->y = 0;
|
|
|
|
// SDL_RenderFillRect(app.renderer, rect);
|
|
|
|
SDL_RenderPresent(app.renderer);
|
|
}
|
|
|
|
//free(cell);
|
|
}
|
|
|
|
void Cleanup(void) {
|
|
SDL_DestroyWindow(app.window);
|
|
SDL_DestroyRenderer(app.renderer);
|
|
TTF_CloseFont(app.font);
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
}
|
|
|
|
void Thing(int x, int y) {
|
|
x = x / CELL_SIZE;
|
|
y = y /CELL_SIZE;
|
|
|
|
if (GlobalList->cells[GetIndex(x, y, MAX_X)] == NULL) {
|
|
GlobalList->cells[GetIndex(x, y, MAX_X)] = CreateCell(x, y);
|
|
GlobalList->size++;
|
|
}
|
|
else {
|
|
printf("Cell already at [%d, %d]\n", x, y);
|
|
}
|
|
}
|
|
|
|
void HandleInput(void) {
|
|
SDL_Event event;
|
|
while(SDL_PollEvent(&event)) {
|
|
switch (event.type) {
|
|
case SDL_QUIT:
|
|
exit(0);
|
|
break;
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
if (event.button.button == SDL_BUTTON_RIGHT) exit(0);
|
|
else Thing(event.button.x, event.button.y);
|
|
case SDL_KEYDOWN:
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
int GetIndex(int x, int y, int columns) {
|
|
return x + y * columns;
|
|
}
|
|
|
|
int GetNotSoRandomNumber(int max) {
|
|
return rand() % (max + 1);
|
|
} |