Finish text render test
This commit is contained in:
parent
deb02fa359
commit
8a16eff9ba
BIN
FreeSans.ttf
Normal file
BIN
FreeSans.ttf
Normal file
Binary file not shown.
@ -6,7 +6,7 @@ pub fn build(b: *Builder) void {
|
||||
|
||||
exe.setBuildMode(mode);
|
||||
exe.linkSystemLibrary("SDL2");
|
||||
exe.linkSystemLibrary("SDL_TTF");
|
||||
exe.linkSystemLibrary("SDL2_TTF");
|
||||
exe.linkSystemLibrary("c");
|
||||
|
||||
b.default_step.dependOn(&exe.step);
|
||||
|
59
src/main.zig
59
src/main.zig
@ -1,11 +1,8 @@
|
||||
const std = @import("std");
|
||||
|
||||
const c = @cImport({
|
||||
@cInclude("SDL2/SDL.h");
|
||||
});
|
||||
|
||||
const t = @cImport({
|
||||
@cInclude("SDL/SDL_ttf.h");
|
||||
@cInclude("SDL.h");
|
||||
@cInclude("SDL_ttf.h");
|
||||
});
|
||||
|
||||
const assert = @import("std").debug.assert;
|
||||
@ -36,18 +33,55 @@ pub fn main() !void {
|
||||
|
||||
const source_code = @embedFile("GapProbeScan.cs");
|
||||
|
||||
const color_result = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
const color_result: c_int = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
if (color_result != 0) {
|
||||
c.SDL_Log("Unable to set draw color: %d", color_result);
|
||||
return error.SDLInitializationFailed;
|
||||
}
|
||||
|
||||
const ttf_init_result = t.TTF_Init();
|
||||
// https://github.com/cirosantilli/cpp-cheat/blob/e52ed4b838e2697d8f44ab5bef3e7a170705d48e/sdl/ttf.c
|
||||
const ttf_init_result: c_int = c.TTF_Init();
|
||||
if (ttf_init_result != 0) {
|
||||
c.SDL_Log("Unable to init TTF: %d", ttf_init_result);
|
||||
return error.TTFInitializationFailed;
|
||||
}
|
||||
defer t.TTF_Quit();
|
||||
defer c.TTF_Quit();
|
||||
|
||||
const text_color: c.SDL_Color = c.SDL_Color {
|
||||
.r = 0,
|
||||
.g = 0,
|
||||
.b = 0,
|
||||
.a = 255
|
||||
};
|
||||
|
||||
// TODO: can we embed this file?
|
||||
const font: ?*c.TTF_Font = c.TTF_OpenFont("FreeSans.ttf", 24);
|
||||
if (font == null) {
|
||||
const err = c.TTF_GetError();
|
||||
c.SDL_Log("Unable to load font: %s", err);
|
||||
return error.TTFFontLoadFailed;
|
||||
}
|
||||
|
||||
const surface: [*c]c.SDL_Surface = c.TTF_RenderText_Solid(font, "Hello, World!", text_color);
|
||||
if (surface == null) {
|
||||
c.SDL_Log("Unable to render text");
|
||||
return error.RenderTextFailed;
|
||||
}
|
||||
defer c.SDL_FreeSurface(surface);
|
||||
|
||||
const texture: ?*c.SDL_Texture = c.SDL_CreateTextureFromSurface(renderer, surface);
|
||||
if (texture == null) {
|
||||
c.SDL_Log("Unable to create texture from surface");
|
||||
return error.CreateTextureFailed;
|
||||
}
|
||||
defer c.SDL_DestroyTexture(texture);
|
||||
|
||||
const rect = c.SDL_Rect {
|
||||
.x = 0,
|
||||
.y = 0,
|
||||
.w = surface.*.w,
|
||||
.h = surface.*.h,
|
||||
};
|
||||
|
||||
// const surface = c.SDL_GetWindowSurface(screen);
|
||||
|
||||
@ -153,6 +187,15 @@ pub fn main() !void {
|
||||
x += 1;
|
||||
}
|
||||
|
||||
// Display text
|
||||
if (c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255) != 0) {
|
||||
c.SDL_Log("Unable to set draw color");
|
||||
}
|
||||
|
||||
if (c.SDL_RenderCopy(renderer, texture, null, &rect) != 0) {
|
||||
c.SDL_Log("Unable to render copy");
|
||||
}
|
||||
|
||||
c.SDL_RenderPresent(renderer);
|
||||
|
||||
c.SDL_Delay(17);
|
||||
|
Loading…
Reference in New Issue
Block a user