Halfway done

This commit is contained in:
Senad Uka
2022-11-27 18:57:45 +01:00
parent 8f158db4a9
commit 7d0d4881f5
13 changed files with 178 additions and 98 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
.idea/lettergame.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/lettergame.iml" filepath="$PROJECT_DIR$/.idea/lettergame.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -35,11 +35,11 @@
#include <stdbool.h>
#include <SDL.h>
#include "sdlhelpers.h"
#include "utils.h"
#define GRID_MAX_X_CELLS (20)
#define GRID_MAX_Y_CELLS (20)
#define GRID_MAX_X_CELLS (15)
#define GRID_MAX_Y_CELLS (15)
struct Cell
{

28
include/letter.h Normal file
View File

@@ -0,0 +1,28 @@
#ifndef LETTER_H
#define LETTER_H
#include <stdio.h>
#include <stdbool.h>
#include <SDL.h>
#include "sdlhelpers.h"
#include "utils.h"
struct Letter
{
SDL_Rect rect;
// Grid background color
SDL_Color backgroundColor;
// Grid border thickness and color
unsigned int border;
SDL_Color borderColor;
int isFromPreviousMove;
int letterIndex;
};
#endif // LETTER_H

13
include/sdlhelpers.h Normal file
View File

@@ -0,0 +1,13 @@
//
// Created by hamo on 11/25/22.
//
#include <SDL2/SDL.h>
#ifndef LETTERGAME_SDLHELPERS_H
#define LETTERGAME_SDLHELPERS_H
int
SDH_fill_rounded_box_b( SDL_Surface* dst, int xo, int yo,
int w, int h, int r, Uint32 color );
Uint32 ColourToUint(int R, int G, int B);
SDL_Colour UintToColour(Uint32 colour);
#endif //LETTERGAME_SDLHELPERS_H

View File

@@ -1,33 +1,3 @@
/*
* Copyright (c) 2018, 2019 Amine Ben Hassouna <amine.benhassouna@gmail.com>
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#include "game.h"
bool Game_start(SDL_Renderer *renderer, int w, int h)
@@ -41,15 +11,15 @@ bool Game_start(SDL_Renderer *renderer, int w, int h)
grid.rect.h = grid.rect.w;
// Set grid backgroud
grid.backgroundColor = COLOR_DARK_GRAY;
grid.backgroundColor = COLOR_LIGHT_GRAY;
// Set grid border thickness and color
grid.border = 3;
grid.borderColor = COLOR_GRAY;
grid.border = 1;
grid.borderColor = COLOR_BLACK;
// Set number of cells
grid.xCells = 10;
grid.yCells = 10;
grid.xCells = 15;
grid.yCells = 15;
// Set cells border thickness and color
grid.cellsBorder = grid.border;
@@ -71,12 +41,8 @@ bool Game_start(SDL_Renderer *renderer, int w, int h)
SDL_setFramerate(&fpsmanager, 30);
// Initialize start time (in ms)
long long last = Utils_time();
// long long last = Utils_time();
// Falling brick coordinates
int fallingBrickX = grid.xCells / 2;
int fallingBrickY = -1;
int fallingBrickSpeed = 4;
// Event loop exit flag
@@ -103,63 +69,12 @@ bool Game_start(SDL_Renderer *renderer, int w, int h)
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_RIGHT:
if(fallingBrickY != -1 && fallingBrickX < grid.xCells - 1)
{
// Un-color last position
grid.cells[fallingBrickX][fallingBrickY].rectColor = grid.backgroundColor;
// Color new position
fallingBrickX++;
grid.cells[fallingBrickX][fallingBrickY].rectColor = COLOR_RED;
}
break;
case SDLK_LEFT:
if(fallingBrickY != -1 && fallingBrickX > 0)
{
// Un-color last position
grid.cells[fallingBrickX][fallingBrickY].rectColor = grid.backgroundColor;
// Color new position
fallingBrickX--;
grid.cells[fallingBrickX][fallingBrickY].rectColor = COLOR_RED;
}
break;
}
}
}
// Move the falling brick
if(Utils_time() - last >= 1000 / fallingBrickSpeed)
{
if(fallingBrickY >= 0)
{
// Un-color the falling brick last position
grid.cells[fallingBrickX][fallingBrickY].rectColor = grid.backgroundColor;
}
if(fallingBrickY < grid.yCells - 1)
{
// Go to next position
fallingBrickY++;
// Color the falling brick new position
grid.cells[fallingBrickX][fallingBrickY].rectColor = COLOR_RED;
}
else
{
// Reset position
fallingBrickY = -1;
}
last = Utils_time();
}
// Set background color
Utils_setBackgroundColor(renderer, COLOR_DARK_GRAY);
// if(Utils_time() - last >= 1000 / fallingBrickSpeed)
// Render grid
Grid_render(&grid, renderer);

View File

@@ -112,6 +112,7 @@ void Grid_initCell(Grid *grid, Cell *cell, int i, int j, SDL_Color color, SDL_Co
cell->borderColor = borderColor;
}
void Grid_render(Grid *grid, SDL_Renderer *renderer)
{
if(grid->border != 0) // Grid border thickness different from 0
@@ -123,6 +124,13 @@ void Grid_render(Grid *grid, SDL_Renderer *renderer)
grid->borderColor.b,
grid->borderColor.a);
/* (renderer,
grid->rect.x,
grid->rect.y,
grid->rect.w,
grid->rect.h,
grid->borderRadius,
grid->border); */
// Render grid border
SDL_RenderFillRect(renderer, &(grid->rect));
}
@@ -149,7 +157,9 @@ void Grid_renderCell(Cell *cell, SDL_Renderer *renderer)
cell->borderColor.a);
// Render filled cell
SDL_RenderFillRect(renderer, &(cell->border));
// SDL_RenderFillRect(renderer, &(cell->border));
SDH_fill_rounded_box_b(renderer->surface, cell->border.x, cell->border.y, cell->border.w, cell->border.h, 10.,
ColourToUint(cell->borderColor.r, cell->borderColor.g, cell->borderColor.b) );
}
// Set renderer color to cell color
@@ -161,4 +171,5 @@ void Grid_renderCell(Cell *cell, SDL_Renderer *renderer)
// Render filled cell
SDL_RenderFillRect(renderer, &(cell->rect));
}

3
src/letter.c Normal file
View File

@@ -0,0 +1,3 @@
#include "letter.h"

View File

@@ -36,8 +36,8 @@
#include "game.h"
// Define screen dimensions
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
int main(int argc, char* argv[])
{

82
src/sdlhelpers.c Normal file
View File

@@ -0,0 +1,82 @@
//
// Created by hamo on 11/25/22.
//
#include "sdlhelpers.h"
int
SDH_fill_rounded_box_b( SDL_Surface* dst, int xo, int yo,
int w, int h, int r, Uint32 color ) {
int yd = dst->pitch / dst->format->BytesPerPixel;
Uint32* pixels = NULL;
int x,y,i,j;
int rpsqrt2 = (int) (r / sqrt( 2 ) );
double r2 = r*r;
w /= 2;
h /= 2;
xo += w;
yo += h;
w -= r;
h -= r;
if( w < 0 || h < 0 )
return 0;
SDL_LockSurface( dst );
pixels = (Uint32*)( dst->pixels );
int sy = (yo-h)*yd;
int ey = (yo+h)*yd;
int sx = (xo-w);
int ex = (xo+w);
for( i = sy; i<=ey; i+=yd )
for( j = sx-r; j<=ex+r; j++ )
pixels[i+j] = color;
int d = -r;
int x2m1 = -1;
y = r;
for( x=0; x <= rpsqrt2; x++ ) {
x2m1 += 2;
d+= x2m1;
if( d >= 0 ) {
y;
d -= (y*2);
}
for( i=sx-x; i<=ex+x; i++ )
pixels[sy-y*yd + i] = color;
for( i=sx-y; i<=ex+y; i++ )
pixels[sy-x*yd + i] = color;
for( i=sx-y; i<=ex+y; i++ )
pixels[ey+x*yd + i] = color;
for( i=sx-x; i<=ex+x; i++ )
pixels[ey+y*yd + i] = color;
}
SDL_UnlockSurface( dst );
return 1;
}
Uint32 ColourToUint(int R, int G, int B)
{
return (Uint32)((R << 16) + (G << 8) + (B << 0));
}
SDL_Colour UintToColour(Uint32 colour)
{
SDL_Colour tempcol;
tempcol.a = 255;
tempcol.r = (colour >> 16) & 0xFF;
tempcol.g = (colour >> 8) & 0xFF;
tempcol.b = colour & 0xFF;
return tempcol;
}