painfull-success-cg/src/level.c

55 lines
1.5 KiB
C

/* SPDX-License-Identifier: MIT
* Copyright (c) 2021 KikooDX
* This file is part of
* [Painfull Success CG](https://git.sr.ht/~kikoodx/painfull-success-cg),
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/display.h>
#include "lazyint.h"
#include "level.h"
#include "tiles.h"
#include "conf.h"
#include "vec2.h"
void level_draw(Level level) {
/* Pixel position (where we draw). */
u16 x = DRAW_OFFSET_X;
u16 y = DRAW_OFFSET_Y;
/* Cursor position. */
u8 cx = 0;
while (cx < LEVEL_WIDTH) {
u8 cy = 0;
while (cy < LEVEL_HEIGHT) {
const tile_t tile = level.content[cy * LEVEL_WIDTH + cx];
const u32 color = tile_color(tile);
drect(x, y, x + TILE_SIZE - 1, y + TILE_SIZE - 1, color);
y += TILE_SIZE;
cy += 1;
}
y = DRAW_OFFSET_Y;
x += TILE_SIZE;
cx += 1;
}
}
tile_t level_get_tile_at_px(Level level, Vec2 pos) {
/* Out of bounds check. */
if (pos.x < 0 || pos.y < 0)
return OUT_OF_BOUNDS;
/* Convert to unsigned integers. */
u8 x = pos.x / TILE_SIZE;
u8 y = pos.y / TILE_SIZE;
/* Out of bounds check. */
if (pos.x >= LEVEL_WIDTH * TILE_SIZE || pos.y >= LEVEL_HEIGHT * TILE_SIZE)
return OUT_OF_BOUNDS;
/* If everything is OK, get the tile and return it. */
return level.content[x + y * LEVEL_WIDTH];
}
void level_set_tile_at_px(Level *level, Vec2 pos, tile_t tile) {
/* Convert to unsigned integers. */
u8 x = pos.x / TILE_SIZE;
u8 y = pos.y / TILE_SIZE;
level->content[x + y * LEVEL_WIDTH] = tile;
}