Skip to content

Commit d9e960d

Browse files
committed
Render A
1 parent 63282f9 commit d9e960d

File tree

6 files changed

+148
-3
lines changed

6 files changed

+148
-3
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ BUILD := build
66
BUILD_TYPE := Debug
77
BIN := $(BUILD)/CachedRenderer
88

9-
SOURCES := src/main.c vendor/miniwin/miniwin.c vendor/stb_truetype/impl.c
9+
SOURCES := src/main.c src/renderer.c vendor/miniwin/miniwin.c vendor/stb_truetype/impl.c
1010
OBJECTS := $(SOURCES:.c=.c.o)
1111
OBJECTS := $(patsubst %,$(BUILD)/%,$(OBJECTS))
1212
DEPENDS := $(OBJECTS:.o=.d)

data/fonts/NotoSansMono.ttf

396 KB
Binary file not shown.

src/endian.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef ENDIAN_H_INCLUDED_
2+
#define ENDIAN_H_INCLUDED_ 1
3+
4+
#if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
5+
#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) || \
6+
(defined(__BYTE_ORDER) && __BYTE_ORDER == __BIG_ENDIAN) || \
7+
(defined(_BYTE_ORDER) && _BYTE_ORDER == _BIG_ENDIAN) || \
8+
(defined(BYTE_ORDER) && BYTE_ORDER == BIG_ENDIAN) || \
9+
(defined(__sun) && defined(__SVR4) && defined(_BIG_ENDIAN)) || \
10+
defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
11+
defined(_MIBSEB) || defined(__MIBSEB) || defined(__MIBSEB__) || \
12+
defined(_M_PPC)
13+
#define __BIG_ENDIAN__
14+
#elif (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || /* gcc */\
15+
(defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) /* linux header */ || \
16+
(defined(_BYTE_ORDER) && _BYTE_ORDER == _LITTLE_ENDIAN) || \
17+
(defined(BYTE_ORDER) && BYTE_ORDER == LITTLE_ENDIAN) /* mingw header */ || \
18+
(defined(__sun) && defined(__SVR4) && defined(_LITTLE_ENDIAN)) || /* solaris */ \
19+
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
20+
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
21+
defined(_M_IX86) || defined(_M_X64) || defined(_M_IA64) || /* msvc for intel processors */ \
22+
defined(_M_ARM) /* msvc code on arm executes in little endian mode */
23+
#define __LITTLE_ENDIAN__
24+
#endif
25+
#endif
26+
27+
#if !defined(__LITTLE_ENDIAN__) & !defined(__BIG_ENDIAN__)
28+
#error "UNKNOWN Platform / endianness. Configure endianness checks for this platform or set explicitly."
29+
#endif
30+
31+
#endif // !ENDIAN_H_INCLUDED_
32+

src/main.c

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
#include <stdio.h>
12
#include <stdlib.h>
3+
4+
#include "renderer.h"
5+
#include "stb_truetype/stb_truetype.h"
26
#include "miniwin/miniwin.h"
37

8+
typedef struct {
9+
stbtt_fontinfo fInfo;
10+
uint8_t* ttfRawBin;
11+
} CFont;
12+
13+
int LoadFont(CFont* font, const char* filePath);
14+
void FreeFont(CFont* font);
15+
416
int main(int argc, char** argv) {
17+
CFont font = {};
18+
if (LoadFont(&font, "data/fonts/NotoSansMono.ttf") != 0) {
19+
return 1;
20+
}
21+
522
struct MiniWin win;
623
win.title = "CachedRenderer";
724
win.width = 320;
825
win.height = 240;
926

1027
mwin_init(&win);
1128

29+
int bW = 0, bH = 0;
30+
uint8_t* bitMap = stbtt_GetCodepointBitmap(&font.fInfo, 0, stbtt_ScaleForPixelHeight(&font.fInfo, 150), 'A', &bW, &bH, NULL, NULL);
31+
1232
int isRunning = 1;
1333
while (isRunning) {
1434
MW_Event ev;
@@ -22,14 +42,63 @@ int main(int argc, char** argv) {
2242
}
2343
}
2444

25-
for (unsigned int i = 0; i < win.width * win.height; i++) {
26-
win.pixels[i] = rand();
45+
for (uint32_t y = 0; y < bH; y++) {
46+
for (uint32_t x = 0; x < bW; x++) {
47+
sRGBA bCol = { 255, 255, 255, 255 };
48+
bCol.a = bitMap[(y * bW) + x];
49+
sRGBA out = BlendAlpha(bCol, UINT32_TO_RGBA(win.pixels[(y * win.width) + x]), 255);
50+
win.pixels[(y * win.width) + x] = RGBA_TO_UINT32(out);
51+
}
2752
}
2853

54+
55+
// for (unsigned int i = 0; i < win.width * win.height; i++) {
56+
// win.pixels[i] = rand();
57+
// }
58+
2959
mwin_swap(&win);
3060
}
3161

62+
stbtt_FreeBitmap(bitMap, NULL);
63+
3264
mwin_destroy(&win);
65+
FreeFont(&font);
3366
return 0;
3467
}
3568

69+
void FreeFont(CFont* font) {
70+
free(font->ttfRawBin);
71+
}
72+
73+
int LoadFont(CFont* font, const char* filePath) {
74+
FILE* f = fopen(filePath, "rb");
75+
if (f == NULL) return 1;
76+
77+
fseek(f, 0, SEEK_END);
78+
size_t fileSize = ftell(f);
79+
fseek(f, 0, SEEK_SET);
80+
81+
font->ttfRawBin = malloc(fileSize);
82+
if (font->ttfRawBin == NULL) {
83+
fclose(f);
84+
return 1;
85+
}
86+
87+
if (fread(font->ttfRawBin, fileSize, 1, f) != 1) {
88+
free(font->ttfRawBin);
89+
fclose(f);
90+
return 1;
91+
}
92+
93+
if (stbtt_InitFont(&font->fInfo, font->ttfRawBin, stbtt_GetFontOffsetForIndex(font->ttfRawBin, 0)) == 0) {
94+
free(font->ttfRawBin);
95+
fclose(f);
96+
return 1;
97+
}
98+
99+
fclose(f);
100+
101+
return 0;
102+
}
103+
104+

src/renderer.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "renderer.h"
2+
3+
// From pixman-combine32.h -> https://gitlab.freedesktop.org/pixman/pixman/-/blob/fdd716109787ef825f9eb88f73447297c43e5c10/pixman/pixman-combine32.h
4+
#define MUL_UN8(a, b, t) ((t) = (a) * (uint16_t)(b) + 0x80, ((((t) >> 8) + (t)) >> 8))
5+
6+
sRGBA BlendAlpha(sRGBA src, sRGBA backdrop, uint8_t opacity) {
7+
if (src.a == 255) return src;
8+
else if (src.a == 0 || opacity == 0) return backdrop;
9+
10+
int32_t t;
11+
src.a = MUL_UN8(src.a, opacity, t);
12+
13+
sRGBA out;
14+
out.a = src.a + backdrop.a - MUL_UN8(src.a, backdrop.a, t);
15+
out.r = backdrop.r + (src.r - backdrop.r) * src.a / out.a;
16+
out.g = backdrop.g + (src.g - backdrop.g) * src.a / out.a;
17+
out.b = backdrop.b + (src.b - backdrop.b) * src.a / out.a;
18+
19+
return out;
20+
}
21+

src/renderer.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef CRENDERER_RENDERER_H_INCLUDED_
2+
#define CRENDERER_RENDERER_H_INCLUDED_ 1
3+
4+
#include <stdint.h>
5+
#include "endian.h"
6+
7+
typedef struct {
8+
#if defined(__LITTLE_ENDIAN__)
9+
uint8_t a, b, g, r;
10+
#elif defined(__BIG_ENDIAN__)
11+
uint8_t r, g, b, a;
12+
#else
13+
#error "Unknown Endian"
14+
#endif
15+
} sRGBA;
16+
17+
#define UINT32_TO_RGBA(val) (*((sRGBA*)&val))
18+
#define RGBA_TO_UINT32(val) (*((uint32_t*)&val))
19+
20+
sRGBA BlendAlpha(sRGBA src, sRGBA backdrop, uint8_t opacity);
21+
22+
#endif // !CRENDERER_RENDERER_H_INCLUDED_
23+

0 commit comments

Comments
 (0)