ultimecia

A ps1 emulator in c
Log | Files | Refs

commit a7c7896e6dd263cca660f033abc6c91b42db4b43
parent c338c94a7b282fe9da78e4ce19dbdf81258f198a
Author: root <root@lunarcry>
Date:   Sun, 29 Sep 2024 09:05:09 +0000

A start for a software renderer??..

Diffstat:
Msrc/renderer.h | 1-
Asrc/sr/sr.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/src/renderer.h b/src/renderer.h @@ -14,7 +14,6 @@ typedef struct _R_Color { u8 b; } R_Color; - typedef struct _RENDERER { SDL_Window* window; SDL_Surface* surface; diff --git a/src/sr/sr.c b/src/sr/sr.c @@ -0,0 +1,91 @@ +/* Based software renderer in one file */ +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <math.h> +#include <SDL2/SDL.h> +#define W 512 +#define H 512 + +typedef void v; typedef uint8_t u8;typedef uint16_t u16;typedef uint32_t u32;typedef uint64_t u64;typedef int8_t i8;typedef int16_t i16;typedef int32_t i32;typedef int64_t i64;typedef uint8_t Bool; +typedef struct { u32 *data; } FB; +typedef struct {u8 b;u8 g;u8 r; u8 a;} C; +FB fb; SDL_Window* win; SDL_Renderer* ren; +C* +new_C(u32 b) +{ + C* c = malloc(sizeof(C)); + c->r = (u8)(b & 0xff); + c->g = (u8)((b >> 8) & 0xff); + c->b = (u8)((b >> 16) & 0xff); + c->a = (u8)((b >> 24) & 0xff); + return c; +}; + +v new_fb() { fb.data = malloc(W*H*sizeof(u32));} +u8 FB_set(int x, int y, C c, i8 *res) { (!fb.data || x<0 || y<0 || x>=W || y>=H) ? (void)(*res = 0) : memcpy(fb.data + ((x + y * W)), &c, 4); } +C* FB_get(int x, int y) { + void* c = (!fb.data || x<0 || y<0 || x>=W || y>=H) ? (void*)0 : (void*)(new_C(fb.data[(x+y*W)])); + return (C*)c; +} +void line(int x0, int y0, int x1, int y1, C color) { + i8 res = 1; + int tmp; + int steep = 0; + if (abs(x0-x1)<abs(y0-y1)) { + tmp = x0; x0 = y0; y0 = tmp; + tmp = x1; x1 = y1; y1 = tmp; + steep = 1; + } + if (x0>x1) { + tmp = x0; x0 = x1; x1 = tmp; + tmp = y0; y0 = y1; y1 = tmp; + } + int dx = x1-x0; + int dy = y1-y0; + int derror2 = abs(dy)*2; + int error2 = 0; + int y = y0; + for (int x=x0; x<=x1; x++) { + steep ? FB_set(y, x, color, &res) : FB_set(x, y, color, &res); + error2 += derror2; + if (error2 > dx) { + y += (y1>y0?1:-1); + error2 -= dx*2; + } + } +} + +void +init_sdl() { + win= SDL_CreateWindow("Ultimecia", 0, 0, 512, 512, SDL_WINDOW_RESIZABLE); + ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE); +} + +void drw_ls() +{ + line(13, 20, 80, 40, (C){0xff, 0xff, 0xff, 0xff}); + line(20, 13, 40, 80, (C){0xff, 0x0, 0x0, 255}); + line(80, 40, 13, 20, (C){0, 255, 0, 100}); +} + +int +main() +{ + SDL_Init(SDL_INIT_VIDEO); + init_sdl(); + + i8 res = 1; + new_fb(); memset(fb.data, 0, (u32)(W*H)*sizeof(u32)); + drw_ls(); + + SDL_Texture* texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, 512, 512); + + SDL_UpdateTexture(texture, NULL, fb.data, W * sizeof(Uint32)); + SDL_RenderClear(ren); + SDL_RenderCopy(ren, texture, NULL, NULL); + SDL_RenderPresent(ren); + SDL_Delay(3000); + SDL_Quit(); +}