Skip to content

Commit e85c597

Browse files
committed
Merge branch 'snake'
2 parents 95693a9 + 3bea14c commit e85c597

File tree

10 files changed

+2982
-1
lines changed

10 files changed

+2982
-1
lines changed
File renamed without changes.

examples/tsoding_snake/stb_sprintf.h

Lines changed: 1906 additions & 0 deletions
Large diffs are not rendered by default.

examples/tsoding_snake/tsoding_snake.c

Lines changed: 896 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef TSODING_SNAKE_H_
2+
#define TSODING_SNAKE_H_
3+
4+
#include <stddef.h>
5+
6+
typedef unsigned char u8;
7+
typedef unsigned int u32;
8+
typedef unsigned long long u64;
9+
typedef int i32;
10+
typedef int b32;
11+
typedef float f32;
12+
13+
void platform_fill_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
14+
void platform_stroke_rect(i32 x, i32 y, i32 w, i32 h, u32 color);
15+
void platform_fill_text(i32 x, i32 y, const char *text, u32 size, u32 color);
16+
u32 platform_text_width(const char *text, u32 size);
17+
void platform_panic(const char *file_path, i32 line, const char *message);
18+
void platform_log(const char *message);
19+
20+
void game_init(u32 width, u32 height);
21+
void game_resize(u32 width, u32 height);
22+
void game_render(void);
23+
void game_update(f32 dt);
24+
void game_keydown(int key);
25+
26+
#endif // TSODING_SNAKE_H_

fonts/AnekLatin-Light.ttf

108 KB
Binary file not shown.

fonts/OFL.txt

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2021 The Anek Project Authors (https://github.com/EkType/Anek)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<canvas id="game"></canvas>
6868
<script>
6969
const wasmPaths = {
70-
"tsoding": ["game",],
70+
"tsoding": ["tsoding_ball","tsoding_snake",],
7171
"core": ["core_basic_window", "core_basic_screen_manager", "core_input_keys", "core_input_mouse_wheel",],
7272
"shapes": ["shapes_colors_palette"],
7373
"text": ["text_writing_anim"],

raylib.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ function make_environment(env) {
1111
});
1212
}
1313

14+
let iota = 0;
15+
const LOG_ALL = iota++; // Display all logs
16+
const LOG_TRACE = iota++; // Trace logging, intended for internal use only
17+
const LOG_DEBUG = iota++; // Debug logging, used for internal debugging, it should be disabled on release builds
18+
const LOG_INFO = iota++; // Info logging, used for program execution info
19+
const LOG_WARNING = iota++; // Warning logging, used on recoverable failures
20+
const LOG_ERROR = iota++; // Error logging, used on unrecoverable failures
21+
const LOG_FATAL = iota++; // Fatal logging, used to abort program: exit(EXIT_FAILURE)
22+
const LOG_NONE = iota++; // Disable logging
23+
1424
class RaylibJs {
1525
// TODO: We stole the font from the website
1626
// (https://raylib.com/) and it's slightly different than
@@ -192,6 +202,22 @@ class RaylibJs {
192202
return args[0];
193203
}
194204

205+
TraceLog(logLevel, text_ptr, ... args) {
206+
// TODO: Implement printf style formatting for TraceLog
207+
const buffer = this.wasm.instance.exports.memory.buffer;
208+
const text = cstr_by_ptr(buffer, text_ptr);
209+
switch(logLevel) {
210+
case LOG_ALL: console.log(`ALL: ${text} ${args}`); break;
211+
case LOG_TRACE: console.log(`TRACE: ${text} ${args}`); break;
212+
case LOG_DEBUG: console.log(`DEBUG: ${text} ${args}`); break;
213+
case LOG_INFO: console.log(`INFO: ${text} ${args}`); break;
214+
case LOG_WARNING: console.log(`WARNING: ${text} ${args}`); break;
215+
case LOG_ERROR: console.log(`ERROR: ${text} ${args}`); break;
216+
case LOG_FATAL: throw new Error(`FATAL: ${text}`);
217+
case LOG_NONE: console.log(`NONE: ${text} ${args}`); break;
218+
}
219+
}
220+
195221
GetMousePosition(result_ptr) {
196222
const bcrect = this.ctx.canvas.getBoundingClientRect();
197223
const x = this.currentMousePosition.x - bcrect.left;
@@ -284,6 +310,40 @@ class RaylibJs {
284310
this.ctx.drawImage(this.images[id], posX, posY);
285311
}
286312

313+
// TODO: codepoints are not implemented
314+
LoadFontEx(result_ptr, fileName_ptr/*, fontSize, codepoints, codepointCount*/) {
315+
const buffer = this.wasm.instance.exports.memory.buffer;
316+
const fileName = cstr_by_ptr(buffer, fileName_ptr);
317+
// TODO: dynamically generate the name for the font
318+
// Support more than one custom font
319+
const font = new FontFace("myfont", `url(${fileName})`);
320+
document.fonts.add(font);
321+
font.load();
322+
}
323+
324+
GenTextureMipmaps() {}
325+
SetTextureFilter() {}
326+
327+
MeasureTextEx(result_ptr, font, text_ptr, fontSize, spacing) {
328+
const buffer = this.wasm.instance.exports.memory.buffer;
329+
const text = cstr_by_ptr(buffer, text_ptr);
330+
const result = new Float32Array(buffer, result_ptr, 2);
331+
this.ctx.font = fontSize+"px myfont";
332+
const metrics = this.ctx.measureText(text)
333+
result[0] = metrics.width;
334+
result[1] = fontSize;
335+
}
336+
337+
DrawTextEx(font, text_ptr, position_ptr, fontSize, spacing, tint_ptr) {
338+
const buffer = this.wasm.instance.exports.memory.buffer;
339+
const text = cstr_by_ptr(buffer, text_ptr);
340+
const [posX, posY] = new Float32Array(buffer, position_ptr, 2);
341+
const tint = getColorFromMemory(buffer, tint_ptr);
342+
this.ctx.fillStyle = tint;
343+
this.ctx.font = fontSize+"px myfont";
344+
this.ctx.fillText(text, posX, posY + fontSize);
345+
}
346+
287347
raylib_js_set_entry(entry) {
288348
this.entryFunction = this.wasm.instance.exports.__indirect_function_table.get(entry);
289349
}
File renamed without changes.

wasm/tsoding_snake.wasm

78.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)