Skip to content

Commit

Permalink
Write a little test
Browse files Browse the repository at this point in the history
  • Loading branch information
tcdude committed Aug 3, 2024
1 parent 1b0331c commit 5fb5953
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 0 deletions.
81 changes: 81 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: true
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 100
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: false
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 4
UseCRLF: false
UseTab: ForIndentation
20 changes: 20 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: clang-format Check

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
formatting-check:
name: Formatting Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run clang-format style check for C/C++/Protobuf programs.
uses: jidicula/[email protected]
with:
clang-format-version: '13'
32 changes: 32 additions & 0 deletions .github/workflows/linux-opengl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Linux (OpenGL)

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Apt Update
run: sudo apt update
- name: Apt Install
run: sudo apt-get install libasound2-dev libxinerama-dev libxrandr-dev libgl1-mesa-dev libxi-dev libxcursor-dev libudev-dev libwayland-dev wayland-protocols libxkbcommon-dev ninja-build --yes --quiet
- name: Get ImageMagick and xvfb
run: sudo apt install imagemagick xvfb
- name: Get Submodules
run: ./get_dlc
- name: Compile
run: ./krink/Kinc/make -g opengl --compile
- name: Run Test 1
working-directory: ./tests/bin
run: xvfb-run ./krass
- name: Check Test 1
run: compare-im6 -metric mae tests/compare/basic.png tests/bin/basic.png difference.png
27 changes: 27 additions & 0 deletions .github/workflows/windows-direct3d11.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Windows (Direct3D 11)

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:

runs-on: windows-latest

steps:
- uses: actions/checkout@v4
- name: Get Submodules
run: .\get_dlc.bat
- name: Get ImageMagick
run: |
choco install -y imagemagick.app --no-progress
Get-ChildItem -Path "${env:ProgramFiles}" | Where-Object {($_.Name -Like 'ImageMagick*')} | % { $_.FullName } | Out-File -Append -FilePath $env:GITHUB_PATH -Encoding utf8
- name: Compile and run Test 1
run: .\make.bat -v vs2022 -g direct3d11 --run
- name: Check Test 1
run: magick compare -metric mae .\tests\compare\basic.png .\tests\bin\basic.png difference.png
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tests/bin/*
!tests/bin/*.ttf
!tests/bin/*.k
4 changes: 4 additions & 0 deletions get_dlc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
echo -e
git -C `dirname "$0"` submodule update --init
`dirname "$0"`/krink/get_dlc
4 changes: 4 additions & 0 deletions get_dlc.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@pushd "%~dp0"
@git submodule update --init
@krink\get_dlc.bat
@popd
23 changes: 23 additions & 0 deletions kfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let project = new Project('krass');

project.useAsLibrary = () => {
project.debugDir = null;
project.addExclude('tests/basic.c');
};

project.addFile('src/krass.c');
project.addFile('tests/basic.c');
project.addIncludeDir('src');
project.setDebugDir('tests/bin');

if (typeof noKrinkPlease === 'undefined') {
await project.addProject('krink');
}

project.addDefine("KR_FULL_RGBA_FONTS");

project.setCStd('c99');
project.setCppStd('c++11');
project.flatten();

resolve(project);
161 changes: 161 additions & 0 deletions tests/basic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include <kinc/graphics4/graphics.h>
#include <kinc/graphics4/rendertarget.h>
#include <kinc/system.h>
#include <krink/graphics2/graphics.h>
#include <krink/graphics2/ttf.h>
#include <krink/memory.h>
#include <krink/system.h>

#include <krass.h>

#include <assert.h>
#include <stdlib.h>

#ifdef NDEBUG
#define STB_IMAGE_WRITE_IMPLEMENTATION
#endif
#include <internal/stb_image_write.h>

#define WINDOW_WIDTH 512
#define WINDOW_HEIGHT 256
#define FONT_SIZE 24
#define FONT_PATH "B612Mono-Regular.ttf"
#define IMAGE_PATH "tex.k"

enum asset_name {
CIRCLE0 = 0,
CIRCLE1,
CIRCLE2,
CIRCLE3,
CIRCLE4,
IMAGE0,
IMAGE1,
IMAGE2,
IMAGE3,
FONT,
ASSET_COUNT
};

static krass_ctx_t *krass_ctx = NULL;
static const char *sample_text[10] = {"abc123", "def4", "ghi56789", "jkl11111111111", "mno",
"pqr", "stu90909", "vwx-=+/", "z98 !!!", "7"};
static uint64_t colors[5] = {0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff, 0xff00ffff};
static int assets[ASSET_COUNT] = {0};
static kr_ttf_font_t *font = NULL;
static kr_image_t image;

static uint8_t *invert_pixels(uint8_t *data, int width, int height) {
uint8_t *inverted_pixels = (uint8_t *)kr_malloc(width * height * 4);
assert(inverted_pixels != NULL);
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
for (int c = 0; c < 4; ++c) {
inverted_pixels[y * width * 4 + x * 4 + c] =
data[(height - 1 - y) * width * 4 + x * 4 + c];
}
}
}
kr_free(data);
return inverted_pixels;
}

static void create_png(kinc_g4_render_target_t *target) {
uint8_t *data = (uint8_t *)kr_malloc(WINDOW_WIDTH * WINDOW_HEIGHT * 4);
assert(data != NULL);
kinc_g4_render_target_get_pixels(target, data);
if (kinc_g4_render_targets_inverted_y())
data = invert_pixels(data, WINDOW_WIDTH, WINDOW_HEIGHT);
stbi_write_png("basic.png", WINDOW_WIDTH, WINDOW_HEIGHT, 4, data, WINDOW_WIDTH * 4);
kr_free(data);
}

static void update(void *unused) {
kinc_g4_begin(0);
kinc_g4_render_target_t target;
kinc_g4_render_target_init_with_multisampling(&target, WINDOW_WIDTH, WINDOW_HEIGHT,
KINC_G4_RENDER_TARGET_FORMAT_32BIT, 16, 0, 1);
kinc_g4_render_target_t *targets = {&target};
kinc_g4_set_render_targets(&targets, 1);
kinc_g4_clear(KINC_G4_CLEAR_COLOR, 0x0, 0, 0);
kr_g2_begin(0);
kr_g2_set_render_target_dim(WINDOW_WIDTH, WINDOW_HEIGHT);
kr_g2_set_color(0xffffffff);
for (int x = 0; x < 16; ++x)
for (int y = 0; y < 8; ++y) {
int id = CIRCLE0 + ((x + y * 16) % 5);
krass_draw(krass_ctx, assets[id], x * 32, y * 32);
}
krass_draw(krass_ctx, assets[IMAGE0], 10, 10);
krass_draw(krass_ctx, assets[IMAGE1], 266, 10);
krass_draw(krass_ctx, assets[IMAGE2], 10, 128);
krass_draw(krass_ctx, assets[IMAGE3], 266, 128);
font = krass_get_font(krass_ctx, assets[FONT]);
kr_g2_set_font(font, FONT_SIZE);
kr_g2_set_color(0xff888888);
for (int i = 0; i < 10; ++i) {
kr_g2_draw_string(sample_text[i], 0, i * (FONT_SIZE + kr_ttf_line_gap(font, FONT_SIZE)) + kr_ttf_baseline(font, FONT_SIZE));
}
kr_g2_reset_render_target_dim();
kr_g2_end();
create_png(&target);
kinc_g4_restore_render_target();
kinc_g4_end(0);
kinc_g4_swap_buffers();
kinc_stop();
}

static void pre_update(void *unused) {
kinc_g4_begin(0);

if (!krass_tick(krass_ctx)) kinc_set_update_callback(update, NULL);

kr_g2_begin(0);
kr_g2_clear(0xff000000);
kr_g2_set_color(0xffffffff);
kr_g2_fill_rect(0, 112, 511 * krass_progress(krass_ctx) + 1, 30);
kr_g2_end();

kinc_g4_end(0);
kinc_g4_swap_buffers();
}

static void circle_cb(int id, float x, float y, void *data) {
uint64_t *color = (uint64_t *)data;
kr_g2_set_color(*color);
kr_g2_draw_sdf_circle(x + 16, y + 16, 16, 0, 0, 2.2f);
}

static void image_cb(int id, float x, float y, void *data) {
uint64_t image_id = (uint64_t)data;
uint64_t xoff = image_id % 2;
uint64_t yoff = image_id / 2;
float sx = xoff * 512;
float sy = yoff * 512;
kr_g2_draw_scaled_sub_image(&image, sx, sy, 512, 512, x, y, 128, 128);
}

int kickstart(int argc, char **argv) {
kinc_init("krass basic", WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL);
kinc_set_update_callback(pre_update, NULL);

void *mem = malloc(10 * 1024 * 1024);
kr_init(mem, 10 * 1024 * 1024, NULL, 0);
kr_g2_init();
kr_image_init(&image);
kr_image_load(&image, IMAGE_PATH, false);
kr_image_generate_mipmaps(&image, 30);
krass_ctx = krass_init(15, 2, 30);
for (int i = 0; i < 5; ++i)
assets[CIRCLE0 + i] = krass_reserve_quad(
krass_ctx, (krass_dim_t){.width = 32, .height = 32}, circle_cb, &colors[i]);
for (int i = 0; i < 4; ++i)
assets[IMAGE0 + i] = krass_reserve_quad(
krass_ctx, (krass_dim_t){.width = 128, .height = 128}, image_cb, (void *)(uint64_t)i);

assets[FONT] = krass_reserve_quad_font(krass_ctx, FONT_PATH, FONT_SIZE, 0);

krass_finalize(krass_ctx);

kinc_start();
return 0;
}
Binary file added tests/bin/B612Mono-Regular.ttf
Binary file not shown.
Binary file added tests/bin/tex.k
Binary file not shown.
Binary file added tests/compare/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5fb5953

Please sign in to comment.