BG implemented

This commit is contained in:
2025-06-11 16:42:00 -05:00
parent e3438cae77
commit 7d8e1051fe
68 changed files with 733 additions and 2013 deletions

View File

@ -27,7 +27,6 @@ target_include_directories(${DUSK_TARGET_NAME}
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
input.c
)
# Subdirs

View File

@ -1,13 +1,10 @@
# Copyright (c) 2025 Dominic Masters
#
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
renderraylib.c
)
# Subdirs
add_subdirectory(draw)
renderimpl.c
)

View File

@ -1,11 +0,0 @@
# Copyright (c) 2025 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
target_sources(${DUSK_TARGET_NAME}
PRIVATE
drawentity.c
drawui.c
)

View File

@ -1,63 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "drawentity.h"
#include "rpg/world/map.h"
void drawEntities() {
uint8_t x, y;
entity_t *ent = MAP.entities;
do {
if(ent->type == ENTITY_TYPE_NULL) {
ent++;
continue;
}
x = ent->x * ENTITY_WIDTH + ent->subX;
y = ent->y * ENTITY_HEIGHT + ent->subY;
switch(ent->dir) {
case ENTITY_DIR_UP:
DrawTriangle(
(Vector2){ x, y + ENTITY_HEIGHT },
(Vector2){ x + ENTITY_WIDTH, y + ENTITY_HEIGHT },
(Vector2){ x + ENTITY_WIDTH / 2, y },
RED
);
break;
case ENTITY_DIR_DOWN:
DrawTriangle(
(Vector2){ x, y },
(Vector2){ x + ENTITY_WIDTH / 2, y + ENTITY_HEIGHT },
(Vector2){ x + ENTITY_WIDTH, y },
RED
);
break;
case ENTITY_DIR_LEFT:
DrawTriangle(
(Vector2){ x + ENTITY_WIDTH, y },
(Vector2){ x, y + ENTITY_HEIGHT / 2 },
(Vector2){ x + ENTITY_WIDTH, y + ENTITY_HEIGHT },
RED
);
break;
case ENTITY_DIR_RIGHT:
DrawTriangle(
(Vector2){ x, y },
(Vector2){ x, y + ENTITY_HEIGHT },
(Vector2){ x + ENTITY_WIDTH, y + ENTITY_HEIGHT / 2 },
RED
);
break;
default:
break;
}
ent++;
} while(ent->type != ENTITY_TYPE_NULL);
}

View File

@ -1,14 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/renderraylib.h"
/**
* Draws all entities on the map.
*/
void drawEntities();

View File

@ -1,27 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "drawui.h"
#include "ui/ui.h"
#include "ui/uitextbox.h"
void drawUITextbox() {
if(!UI_TEXTBOX.visible) return;
DrawRectangle(
UI_TEXTBOX.x, UI_TEXTBOX.y,
UI_TEXTBOX.width, UI_TEXTBOX.height,
Fade(SKYBLUE, 0.5f)
);
DrawTextEx(
FONT,
UI_TEXTBOX.text,
(Vector2){ UI_TEXTBOX.x + 2, UI_TEXTBOX.y + 2 },
FONT.baseSize, 0, BLACK
);
}

View File

@ -1,14 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "display/renderraylib.h"
/**
* Draws the UI textbox.
*/
void drawUITextbox();

View File

@ -0,0 +1,209 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "display/render.h"
#include "assert/assert.h"
#include "util/memory.h"
uint8_t RENDER_BACKGROUND_X = 0;
uint8_t RENDER_BACKGROUND_Y = 0;
uint8_t RENDER_STATUS = 0;
uint8_t RENDER_TILES[RENDER_TILE_COUNT] = { 0 };
uint8_t RENDER_BACKGROUND_TILEMAP[RENDER_BACKGROUND_TILE_COUNT] = { 0 };
Color RENDER_PALETTE[RENDER_PALETTE_COLOR_COUNT] = {
{ 102, 191, 47, 255 },
{ 78, 146, 35, 255 },
{ 48, 89, 22, 255 },
{ 18, 33, 8, 255 }
};
bool_t RENDER_DISPLAY_ON = false;
RenderTexture2D RENDER_BACKBUFFER;
RenderTexture2D RENDER_TILES_TEXTURE;
void renderInit(void) {
InitWindow(RENDER_WIDTH_PIXELS * 4, RENDER_HEIGHT_PIXELS * 4, "Dusk");
SetTargetFPS(60);
SetWindowState(
FLAG_WINDOW_RESIZABLE |
FLAG_WINDOW_HIGHDPI |
FLAG_VSYNC_HINT
);
// Create back buffer for rendering.
RENDER_BACKBUFFER = LoadRenderTexture(
RENDER_WIDTH_PIXELS,
RENDER_HEIGHT_PIXELS
);
// Create texture to hold the tile data.
RENDER_TILES_TEXTURE = LoadRenderTexture(
RENDER_TILE_COUNT * RENDER_TILE_WIDTH,
RENDER_TILE_HEIGHT
);
}
void renderVsync() {
int32_t x, y, i;
BeginDrawing();
if(RENDER_DISPLAY_ON) {
// Update the texture with the new tile data
BeginTextureMode(RENDER_TILES_TEXTURE);
i = 0;
for(i = 0; i < RENDER_TILE_COUNT; i++) {
uint8_t *tile = RENDER_TILES + (i * RENDER_TILE_BYTES_PER_TILE);
// For each pixel in the tile...
for(y = 0; y < RENDER_TILE_HEIGHT; y++) {
uint8_t low = tile[y * RENDER_TILE_BYTES_PER_ROW];
uint8_t high = tile[y * RENDER_TILE_BYTES_PER_ROW + 1];
for(x = 0; x < RENDER_TILE_WIDTH; x++) {
uint8_t loBit = (low >> (7 - x)) & 1;
uint8_t hiBit = (high >> (7 - x)) & 1;
uint8_t paletteIndex = (hiBit << 1) | loBit;
// Draw the pixel to the texture
DrawPixel(
(i * RENDER_TILE_WIDTH) + x,
y,
RENDER_PALETTE[paletteIndex]
);
}
}
}
EndTextureMode();
// Clear the back buffer
BeginTextureMode(RENDER_BACKBUFFER);
ClearBackground(RENDER_PALETTE[0]);
// Render background tiles
i = 0;
for(y = 0; y < RENDER_BACKGROUND_ROWS; y++) {
for(x = 0; x < RENDER_BACKGROUND_COLUMNS; x++) {
// Get the tile index from the tilemap
uint8_t tileIndex = RENDER_BACKGROUND_TILEMAP[i++];
DrawTexturePro(
RENDER_TILES_TEXTURE.texture,
(Rectangle){
.x = ((int32_t)tileIndex) * RENDER_TILE_WIDTH,
.y = 0,
.width = RENDER_TILE_WIDTH,
.height = -RENDER_TILE_HEIGHT
},
(Rectangle){
((int32_t)x * RENDER_TILE_WIDTH) - RENDER_BACKGROUND_X,
((int32_t)y * RENDER_TILE_HEIGHT) - RENDER_BACKGROUND_Y,
RENDER_TILE_WIDTH,
RENDER_TILE_HEIGHT
},
(Vector2){ 0, 0 },
0.0f,
WHITE
);
}
}
// Render the back buffer to the screen
EndTextureMode();
ClearBackground(WHITE);
// Keep aspect and center the render
int32_t renderWidth, renderHeight, renderX, renderY;
const int32_t width = GetScreenWidth();
const int32_t height = GetScreenHeight();
if (RENDER_WIDTH_PIXELS * height > RENDER_HEIGHT_PIXELS * width) {
renderWidth = width;
renderHeight = (RENDER_HEIGHT_PIXELS * width) / RENDER_WIDTH_PIXELS;
renderX = 0;
renderY = (height - renderHeight) / 2;
} else {
renderWidth = (RENDER_WIDTH_PIXELS * height) / RENDER_HEIGHT_PIXELS;
renderHeight = height;
renderX = (width - renderWidth) / 2;
renderY = 0;
}
DrawTexturePro(
RENDER_BACKBUFFER.texture,
(Rectangle) { 0, 0, RENDER_WIDTH_PIXELS, -RENDER_HEIGHT_PIXELS },
(Rectangle) { renderX, renderY, renderWidth, renderHeight },
(Vector2) { 0, 0 },
0.0f,
WHITE
);
}
EndDrawing();
if(WindowShouldClose()) RENDER_STATUS |= RENDER_SHOULD_EXIT;
}
void renderDispose() {
UnloadRenderTexture(RENDER_TILES_TEXTURE);
CloseWindow();
}
void renderDisplayOn(void) {
RENDER_DISPLAY_ON = true;
}
void renderDisplayOff(void) {
RENDER_DISPLAY_ON = false;
}
void set_bkg_data(
const uint8_t index,
const uint8_t count,
const uint8_t *tiles
) {
assertTrue(count > 0, "Count must be greater than zero");
assertNotNull(tiles, "Tiles pointer must not be null");
// Copy data to fake vram
memoryCopy(
&RENDER_TILES[index * RENDER_TILE_BYTES_PER_TILE],
tiles,
count * RENDER_TILE_BYTES_PER_TILE
);
}
void set_bkg_tiles(
const uint8_t xPos,
const uint8_t yPos,
const uint8_t width,
const uint8_t height,
const uint8_t *tiles
) {
uint8_t w = width, h = height, x = xPos, y = yPos;
assertNotNull(tiles, "Tiles pointer must not be null");
// Clamp x and y to tilemap bounds
if (x >= RENDER_BACKGROUND_COLUMNS) x = RENDER_BACKGROUND_COLUMNS - 1;
if (y >= RENDER_BACKGROUND_ROWS) y = RENDER_BACKGROUND_ROWS - 1;
// Clamp width and height so we don't overflow past edges
if (x + w > RENDER_BACKGROUND_COLUMNS) {
w = RENDER_BACKGROUND_COLUMNS - x;
}
if (y + h > RENDER_BACKGROUND_ROWS) {
h = RENDER_BACKGROUND_ROWS - y;
}
if (w == 0 || h == 0) return;
for (uint8_t row = 0; row < h; row++) {
memoryCopy(
&RENDER_BACKGROUND_TILEMAP[(y + row) * RENDER_BACKGROUND_COLUMNS + x],
&tiles[row * width],
w
);
}
}

View File

@ -0,0 +1,56 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "duskraylib.h"
#define RENDER_TILE_COUNT 384
#define RENDER_TILE_WIDTH 8
#define RENDER_TILE_BYTES_PER_ROW 2
#define RENDER_TILE_HEIGHT 8
#define RENDER_TILE_BYTES_PER_TILE (RENDER_TILE_BYTES_PER_ROW * RENDER_TILE_HEIGHT)
#define RENDER_BACKGROUND_COLUMNS 32
#define RENDER_BACKGROUND_ROWS 32
#define RENDER_BACKGROUND_TILE_COUNT (RENDER_BACKGROUND_COLUMNS * RENDER_BACKGROUND_ROWS)
extern uint8_t RENDER_BACKGROUND_X;
extern uint8_t RENDER_BACKGROUND_Y;
extern uint8_t RENDER_TILES[RENDER_TILE_COUNT];
#define RENDER_PALETTE_COLOR_COUNT 4
extern Color RENDER_PALETTE[RENDER_PALETTE_COLOR_COUNT];
extern bool_t RENDER_DISPLAY_ON;
/**
* Sets the background tile data.
*
* @param index The starting index of the tile data.
* @param count The number of tiles to set.
* @param tiles Pointer to the tile data array.
*/
void set_bkg_data(
const uint8_t index,
const uint8_t count,
const uint8_t *tiles
);
/**
* Sets the background tiles.
*
* @param x The x-coordinate of the top-left corner of the background.
* @param y The y-coordinate of the top-left corner of the background.
* @param width The width of the background in tiles.
* @param height The height of the background in tiles.
* @param tiles Pointer to the tile map data.
*/
void set_bkg_tiles(
const uint8_t x,
const uint8_t y,
const uint8_t width,
const uint8_t height,
const uint8_t *tiles
);

View File

@ -1,49 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "assert/assert.h"
#include "display/render.h"
#include <raylib.h>
#include "display/draw/drawentity.h"
#include "display/draw/drawui.h"
const uint16_t RENDER_WIDTH = 480;
const uint16_t RENDER_HEIGHT = 270;
Font FONT;
void renderInit() {
InitWindow(RENDER_WIDTH, RENDER_HEIGHT, "Dusk");
SetTargetFPS(60);
FONT = LoadFontEx(
"../assets/ark-pixel-font/12px/monospaced/ark-pixel-12px-monospaced-latin.otf",
12, NULL, 0
);
}
bool_t renderUpdate() {
uint8_t x, y;
char_t buffer[64];
// End rendering?
if(WindowShouldClose()) return false;
BeginDrawing();
ClearBackground(RAYWHITE);
drawEntities();
drawUITextbox();
EndDrawing();
return true;
}
void renderDispose() {
CloseWindow();
}

View File

@ -6,7 +6,5 @@
*/
#pragma once
#include "display/render.h"
#include <raylib.h>
extern Font FONT;
#include "dusk.h"
#include <raylib.h>

View File

@ -1,60 +0,0 @@
/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "input.h"
#include "raylib.h"
typedef struct {
int32_t key;
uint8_t flag;
} inputmap_t;
const inputmap_t INPUT_MAP[] = {
{ KEY_W, INPUT_UP },
{ KEY_S, INPUT_DOWN },
{ KEY_A, INPUT_LEFT },
{ KEY_D, INPUT_RIGHT },
{ KEY_SPACE, INPUT_ACTION },
{ KEY_E, INPUT_ACTION },
{ KEY_ENTER, INPUT_ACTION },
{ KEY_ESCAPE, INPUT_CANCEL },
{ KEY_Q, INPUT_CANCEL },
{ KEY_BACKSPACE, INPUT_CANCEL },
{ KEY_UP, INPUT_UP },
{ KEY_DOWN, INPUT_DOWN },
{ KEY_LEFT, INPUT_LEFT },
{ KEY_RIGHT, INPUT_RIGHT },
{ KEY_ENTER, INPUT_ACTION },
{ KEY_BACKSPACE, INPUT_CANCEL },
{ 0, 0 }
};
void inputInit() {
}
void inputUpdate() {
uint8_t state = 0;
inputmap_t *map = INPUT_MAP;
do {
if(IsKeyDown(map->key)) {
state |= map->flag;
}
map++;
} while(map->key != 0);
INPUT.previous = INPUT.current;
INPUT.current = state;
}
void inputDispose() {
}