painfull-success-cg/src/level.c

54 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 <stdint.h>
#include "level.h"
#include "tiles.h"
#include "vec2.h"
void level_draw(Level level) {
/* Pixel position (where we draw). */
uint16_t x = DRAW_OFFSET_X;
uint16_t y = DRAW_OFFSET_Y;
/* Cursor position. */
uint8_t cx = 0;
while (cx < LEVEL_WIDTH) {
uint8_t cy = 0;
while (cy < LEVEL_HEIGHT) {
const tile_t tile = level.content[cy * LEVEL_WIDTH + cx];
const int 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. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
/* Out of bounds check. */
if (pos.x >= LEVEL_WIDTH || pos.y >= LEVEL_HEIGHT)
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. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
level->content[x + y * LEVEL_WIDTH] = tile;
}