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
4 changes: 4 additions & 0 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Install SDL2 dependencies
run: |
sudo apt-get install -y libsdl2-dev

- uses: actions/checkout@v4
- name: make build
run: make build
1,634 changes: 1 addition & 1,633 deletions assets/charset.c

Large diffs are not rendered by default.

1,634 changes: 1 addition & 1,633 deletions assets/charset_debug.c

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions assets/compile_charsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ def compile_charset(filename):

for row in range(8):
for col in range(12):
out += "\n { "
# out += "\n "
out += "{"

for row1 in range(0, 16):
for col1 in range(0, 16):
out += f"{color_to_num(bitmap[row * 16 + row1, col * 16 + col1])}, "
out += "\n "
out += f"{color_to_num(bitmap[row * 16 + row1, col * 16 + col1])},"
# out += "\n "

out = out[:-2]
out += "},"

return out + "}"
Expand Down
1 change: 0 additions & 1 deletion src/graphics/sdl_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
void G_error() {
const char* error = SDL_GetError();

printf("what\n");
printf("SDL error:\n%s\n", error);

SDL_Quit();
Expand Down
13 changes: 0 additions & 13 deletions src/graphics/sdl_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,4 @@ void G_setDrawColor(uint32_t color) {
G_error();
}

/**
* Sets rendering scale such that the video memory takes up the whole window.
*/
void G_strechScreen() {
int width, height;
SDL_GetWindowSize(G_SDLwindow, &width, &height);

float scaleX = (float)width / G_width;
float scaleY = (float)height / G_height;
if (SDL_RenderSetScale(G_SDLrenderer, scaleX, scaleY))
G_error();
}

#endif
75 changes: 29 additions & 46 deletions src/graphics/sdl_text.h → src/graphics/sdl_videomem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,10 @@

#include <graphics/sdl_graphics.h>
#include <graphics/sdl_init.h>
#include <string.h>

SDL_Texture *G_videomem = NULL;

// note: 16x16 charsets might not be ideal
/**
* Writes a character at the specified x and y offset (16 pixel increments).
*
* @param c character index in charset (not ASCII value)
* @param x x character position
* @param y y character position
* @param charset the charset to be used
*/
void G_writeChar(char c, int x, int y, const char charset[][256]) {
for (int j = 0; j < 16; j++) {
for (int k = 0; k < 16; k++) {
G_setDrawColor(0x00FF00FF * charset[c][j + k * 16]);

G_setPixel(j + x * 16, k + y * 16);
}
}
}

/**
* Creates a video memory texture.
*
Expand All @@ -40,7 +22,7 @@ int G_initVideomem(int w, int h) {
G_width = w;
G_height = h;

G_videomem = SDL_CreateTexture(G_SDLrenderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, w, h);
G_videomem = SDL_CreateTexture(G_SDLrenderer, SDL_PIXELFORMAT_ABGR32, SDL_TEXTUREACCESS_STREAMING, w, h);

if (!G_videomem) {
printf("failed to initialize video memory\n");
Expand All @@ -50,21 +32,6 @@ int G_initVideomem(int w, int h) {
return 0;
}

/**
* Sets the video memory as a render target.
*
* @return `0` on success, `1` if video memory doesn't exist. Calls `G_error` on error.
*/
int G_useVideomem() {
if (!G_videomem)
return 1;

if (SDL_SetRenderTarget(G_SDLrenderer, G_videomem))
G_error();

return 0;
}

/**
* Renders the video memory.
*
Expand All @@ -76,17 +43,6 @@ int G_render() {
if (!G_videomem)
return 1;

// Update screen
G_strechScreen();

if (SDL_RenderCopy(G_SDLrenderer, G_videomem, NULL, NULL))
G_error();

SDL_RenderPresent(G_SDLrenderer);

if (SDL_SetRenderTarget(G_SDLrenderer, NULL))
G_error();

// Draw video mem
if (SDL_RenderCopy(G_SDLrenderer, G_videomem, NULL, NULL))
G_error();
Expand All @@ -109,4 +65,31 @@ void G_deleteVideomem() {
G_videomem = NULL;
}

// note: 16x16 charsets might not be ideal
/**
* Writes a character at the specified x and y offset (16 pixel increments).
*
* @param x x character position
* @param y y character position
* @param c character entry in charset
* @param color RGBA color of character
*/
void G_writeChar(int x, int y, const char c[256], Uint32 color) {
SDL_Rect rect = { x * 16, y * 16, 16, 16 };

char *pixels;
int pitch;

if (SDL_LockTexture(G_videomem, &rect, (void**)&pixels, &pitch))
G_error();

for (int y = 0; y < 16; y++) {
for (int x = 0; x < 16; x++) {
*(Uint32*)(pixels + x*4 + y*pitch) = color * c[x + y * 16];
}
}

SDL_UnlockTexture(G_videomem);
}

#endif
10 changes: 4 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "graphics/sdl_init.h"
#include "graphics/sdl_graphics.h"
#include "graphics/sdl_error.h"
#include "graphics/sdl_text.h"
#include "graphics/sdl_videomem.h"
#include "charset_debug.c"
#include "charset.c"

#include <stdlib.h>
#include <string.h>

void quit() {
// Delete video memory
Expand Down Expand Up @@ -33,14 +34,11 @@ int main(int argc, char *argv[]) {

// example sdl code
G_initVideomem(640, 480);
G_useVideomem();

G_setDrawColor(0x00FF00FF);

int xOffset = 0, yOffset = 0;

for (int i = 0; i < 96; i++) {
G_writeChar(i, xOffset, yOffset, charset);
G_writeChar(xOffset, yOffset, charset[i], 0x00FF00FF);

if (xOffset++ == 39) {
xOffset = 0;
Expand All @@ -52,7 +50,7 @@ int main(int argc, char *argv[]) {
xOffset = 0;

for (int i = 0; i < 96; i++) {
G_writeChar(i, xOffset, yOffset, charset_debug);
G_writeChar(xOffset, yOffset, charset_debug[i], 0x00FF00FF);

if (xOffset++ == 39) {
xOffset = 0;
Expand Down