Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ int main()
fscanf(level, "%i %i", &x, &y);
fscanf(level, "%i %i", &x_player, &y_player);
fscanf(level, "%u", &camfov);

// Normalize
y_player = y - y_player;
x_player = x_player - 1;

if(x <= 0 || y <= 0)
{
fclose(level);
printf("\nInvalid world dimensions.");
return 0;
}
if(x_player > x || (y - y_player) > y || x_player <= 0 || (y - y_player) <= 0)
if(x_player > x || y_player > y || x_player < 0 || y_player < 0)
{
fclose(level);
printf("\nOut of bounds position.");
Expand Down
16 changes: 15 additions & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
game: main.c render.c
gcc -o game -g main.c render.c
gcc -o game -O2 main.c render.c
gameDEBUG: main.c render.c
gcc -o gameDEBUG -g main.c render.c

gameX64: main.c render.c
gcc -m64 -o gameX64 -O2 main.c render.c
gameX64DEBUG: main.c render.c
gcc -m64 -o gameX64DEBUG -g main.c render.c

gameX32: main.c render.c
gcc -m32 -o gameX32 -O2 main.c render.c
gameX32DEBUG: main.c render.c
gcc -m32 -o gameX32DEBUG -g main.c render.c

all: gameX64 gameX64DEBUG game gameDEBUG
111 changes: 58 additions & 53 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define CYAN "\x1b[46m"
#define RESET "\x1b[0m"

char *color[] = { BLACK, WHITE, RED, GREEN, YELLOW, BROWN, PINK };

/*
. - green color
, - yellow color
Expand All @@ -25,13 +27,16 @@ Maybe will make output as colored dots only

// Maybe give an input value

// General variables
extern int x, y;
extern int x_player, y_player;
extern unsigned int camfov;

void print(int up, int down, int left, int right, char a[y][x])
int up, down, left, right;

void print(char a[y][x])
{
int i, j;
int i, j, colori;

for(j = up; j <= down; j++)
{
Expand All @@ -41,22 +46,25 @@ void print(int up, int down, int left, int right, char a[y][x])
switch (a[j][i])
{
case '.':
printf(GREEN " " RESET);
colori = 3;
break;
case ',':
printf(YELLOW " " RESET);
colori = 4;
break;
case '#':
printf(BROWN " " RESET);
colori = 5;
break;
case '@':
printf(PINK " " RESET);
colori = 6;
break;

default:
printf(BLACK " " RESET);
colori = 0;
break;
}

printf(color[colori]);
printf(" " RESET);
}
printf("\n");
}
Expand All @@ -66,69 +74,66 @@ void print(int up, int down, int left, int right, char a[y][x])

void camera(char a[y][x])
{
// If world is smaller than camera fov
if(x <= camfov - 1 || y <= camfov - 1)
{
// + 1 in order to set correct matrix size
print(0, y - 1, 0, x - 1, a);
return;
}

// Math coordonates to array index
x_player--;
// ypos--;

int up_bound, down_bound;
int left_bound, right_bound;

/*
If camera FOV is positive (10x10 screen)
then place coordonates in top-left pixel.

In the future, if the player will be able
to move, move camera if they leave the
middle 2x2 zone (center).
*/

// x_player--;
// y_player--;

// Y bounds
up_bound = y_player - (camfov/2);
down_bound = y_player + (camfov/2);
up = y_player - (camfov/2);
down = y_player + (camfov/2);
// X bounds
left_bound = x_player - (camfov/2);
right_bound = x_player + (camfov/2);
left = x_player - (camfov/2);
right = x_player + (camfov/2);

if(camfov % 2 == 0)
// Up and Down camera bounds
if(up < 0)
{
up_bound++;
left_bound++;
down = camfov;
up = 0;
}

// Back to bounds math: Negative shift
if(up_bound < 0)
else if(down >= y)
{
down_bound = camfov - 1;
up_bound = 0;
up = y - camfov;
down = y;
}
if(left_bound < 0)
// Left and Right camera bounds
if(left < 0)
{
right_bound = camfov - 1;
left_bound = 0;
right = camfov;
left = 0;
}
else if(right >= x)
{
left = x - camfov;
right = x;
}

// Positive shift
if(down_bound >= y)
// If camera FOV is bigger than world
if(x <= camfov || y <= camfov)
{
up_bound = y - camfov + 1;
down_bound = y;
right = x - 1;
down = y - 1;
print(a);
return;
}
if(right_bound >= x)

/*
If camera FOV is positive (10x10 screen)
then place coordonates in top-left pixel.

In the future, if the player will be able
to move, move camera if they leave the
middle 2x2 zone (center).
*/
if(camfov % 2 == 0)
{
left_bound = x - camfov;
right_bound = x;
up++;
left++;
}

print(up_bound, down_bound, left_bound, right_bound, a);


print(a);

return;
}