This commit is contained in:
kdx 2023-03-17 13:45:20 +01:00
parent b6e8281daa
commit f3f3100dbd
9 changed files with 77 additions and 12 deletions

View File

@ -15,6 +15,7 @@ set(SOURCES
src/game.c
src/lzy.c
src/main.c
src/map.c
src/player.c
)

View File

@ -46,8 +46,8 @@ void
background_draw(void)
{
tick += 1;
LZY_DrawSetColor(0, 0, 0);
draw_square(64 * sin((double)tick / 50), (double)tick / 40);
draw_square(64 * sin((double)tick / 40), (double)tick / 30);
draw_square(64 * sin((double)tick / 30), (double)tick / 20);
LZY_DrawSetColor(BLACK);
draw_square(300 * sin((double)tick / 50), (double)tick / 40);
draw_square(300 * sin((double)tick / 40), (double)tick / 30);
draw_square(300 * sin((double)tick / 30), (double)tick / 20);
}

View File

@ -4,3 +4,5 @@
#define DISPLAY_WIDTH 400
#define DISPLAY_HEIGHT 224
#define TSIZE 16
#define WHITE 255, 255, 255
#define BLACK 0, 0, 0

View File

@ -1,5 +1,6 @@
#include "entity.h"
#include "game.h"
#include "map.h"
#include "cfg.h"
bool
@ -10,7 +11,8 @@ entity_collide(Entity *this, Game *g, int ox, int oy)
const int y0 = this->pos[1] - this->height / 2 + oy;
const int x1 = x0 + this->width - 1;
const int y1 = y0 + this->height - 1;
return (x0 < 0 || y0 < 0 || x1 >= DISPLAY_WIDTH || y1 >= DISPLAY_HEIGHT);
return (map_get_px(x0, y0) == '0' || map_get_px(x0, y1) == '0' ||
map_get_px(x1, y0) == '0' || map_get_px(x1, y1) == '0');
}
void

View File

@ -1,4 +1,5 @@
#include "game.h"
#include "map.h"
#include <string.h>
void
@ -26,6 +27,7 @@ game_update(Game *this)
void
game_draw(Game *this)
{
map_draw();
for (int i = 0; i < MAX_ENTITIES; i++) {
Entity *const e = &this->entities[i];
if (e->type != ET_NONE && e->draw != NULL)

View File

@ -2,6 +2,7 @@
#include "game.h"
#include "player.h"
#include "background.h"
#include "cfg.h"
#include <stdio.h>
#include <stdlib.h>
@ -27,7 +28,7 @@ int main(void)
game_update(game);
LZY_DrawBegin();
LZY_DrawSetColor(255, 255, 255);
LZY_DrawSetColor(WHITE);
LZY_DrawClear();
background_draw();
game_draw(game);

48
src/map.c Normal file
View File

@ -0,0 +1,48 @@
#include "map.h"
#include "lzy.h"
#include "cfg.h"
static const char *map =
"0000000000000000000000000"
"0 0"
"0 0"
"0 0"
"0 0"
"0 0"
"0 0"
"0 0"
"0 0"
"0 0"
"00000000000000000000 0"
"0 0"
"0 0"
"0000000000000000000000000"
;
int
map_get(int x, int y)
{
if (x < 0 || y < 0 || x >= 25 || y >= 14)
return 0;
return map[x + y * 25];
}
int
map_get_px(int x, int y)
{
if (x < 0 || y < 0)
return 0;
return map_get(x / TSIZE, y / TSIZE);
}
void
map_draw(void)
{
for (int y = 0; y < 14; y++)
for (int x = 0; x < 25; x++)
if (map[x + y * 25] == '0') {
LZY_DrawSetColor(BLACK);
LZY_DrawRect(x*16, y*16, 16, 16);
LZY_DrawFillRect(x*16, y*16, 16, 16);
}
}

5
src/map.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
int map_get(int x, int y);
int map_get_px(int x, int y);
void map_draw(void);

View File

@ -2,6 +2,7 @@
#include "entity.h"
#include "game.h"
#include "lzy.h"
#include "cfg.h"
#include <string.h>
static void
@ -16,18 +17,21 @@ static void
player_draw(Entity *this, Game *g)
{
(void)g;
LZY_DrawSetColor(0, 0, 0);
LZY_DrawFillRect(this->pos[0] - this->width / 2,
this->pos[1] - this->height / 2,
this->width, this->height);
LZY_DrawSetColor(BLACK);
LZY_DrawRect(this->pos[0] - this->width / 2,
this->pos[1] - this->height / 2,
this->width, this->height);
LZY_DrawRect(this->pos[0] - this->width / 2 + 1,
this->pos[1] - this->height / 2 + 1,
this->width - 2, this->height - 2);
}
void
player_init(Entity *this)
{
memset(this, 0, sizeof(*this));
this->pos[0] = 16;
this->pos[1] = 16;
this->pos[0] = 32;
this->pos[1] = 32;
this->type = ET_PLAYER;
this->update = player_update;
this->draw = player_draw;