Skip to content

Commit

Permalink
Merge branch 'main' into marek/textbox-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ske2004 committed Mar 11, 2024
2 parents 370a4e7 + 0d814e1 commit 77f0a66
Show file tree
Hide file tree
Showing 31 changed files with 765 additions and 739 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/package.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: Tophat workflow
on: [push]
on:
push:
branches:
- main
jobs:
windows_build:
runs-on: windows-latest
Expand Down
6 changes: 3 additions & 3 deletions examples/flappy/main.um
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ fn drawCenteredText(t: str, y: th.fu, c: uint32, s: th.fu): th.fu {

fn menu() {
// check all the known keys for a press
for i:=0; i <= input.key_menu; i++ {
if input.isJustPressed(i) {
for i:=0; i <= int(input.Key.menu); i++ {
if input.isJustPressed(input.Key(i)) {
play()
}
}
Expand Down Expand Up @@ -196,7 +196,7 @@ fn game() {
}

// jumping means setting the y motion to the jumpForce
if input.isJustPressedc(' ') || input.isJustPressed(input.mouse1) {
if input.isJustPressedc(' ') || input.isJustPressed(input.Key.mouse1) {
motion.y = jumpForce
jumpSound.play()
}
Expand Down
2 changes: 1 addition & 1 deletion examples/platformer/game/env.um
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn reset*() {
}

fn handle*() {
if input.isJustPressed(input.key_enter) {
if input.isJustPressed(input.Key.enter) {
reset()
start = th.time
}
Expand Down
2 changes: 1 addition & 1 deletion examples/platformer/game/game.um
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn handle*() {
bees.handle()
player.handle()
case stateFinished:
if input.isJustPressed(input.key_enter) {
if input.isJustPressed(input.Key.enter) {
state = stateGame
reset()
}
Expand Down
6 changes: 3 additions & 3 deletions examples/platformer/game/player.um
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ fn move() {
canJump = false
}

if input.isPressed(input.key_left) {
if input.isPressed(input.Key.left) {
mot.x = -1
}

if input.isPressed(input.key_right) {
if input.isPressed(input.Key.right) {
mot.x = 1
}

Expand Down Expand Up @@ -150,7 +150,7 @@ fn handle*() {
die()
}

if input.isJustPressed(input.key_escape) {
if input.isJustPressed(input.Key.escape) {
body.t.p = env.spawn
}

Expand Down
8 changes: 4 additions & 4 deletions examples/pomodoro/main.um
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn guiGetButtonState(gui: ^Gui): ButtonState {
lastRect := guiGetLastRect(gui).rect
state := ButtonState{}
state.hovered = rectVsPoint(lastRect, th_input.getMousePos())
state.pressed = state.hovered && th_input.isJustPressed(th_input.mouse1)
state.pressed = state.hovered && th_input.isJustPressed(th_input.Key.mouse1)
return state
}

Expand Down Expand Up @@ -401,7 +401,7 @@ const onFrame = fn(args: []any) {
v_colorscheme = c_colorschemeLight
}

if th_input.isJustPressed(th_input.key_escape) {
if th_input.isJustPressed(th_input.Key.escape) {
debug = !debug
}

Expand Down Expand Up @@ -490,8 +490,8 @@ fn init*() {
v_lastTime = th.time
th_window.setup("Pomodoro", trunc(screen.w), trunc(screen.h))
th_window.setViewport(th.Vf2{screen.w, screen.h})
v_mainFont = th_font.load("Font.ttf", 32, 1)
v_mainFontSmall = th_font.load("Font.ttf", 20, 1)
v_mainFont = th_font.load("Font.ttf", 32)
v_mainFontSmall = th_font.load("Font.ttf", 20)
v_countdown = getCountdown(0)
v_alarm = th_audio.load("Alarm.wav")
v_rectBase = th_image.load("rectbase.png")
Expand Down
2 changes: 1 addition & 1 deletion examples/space-shooter/hud.um
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
fn init*() {
hearth = image.load("gfx/hearth.png")

mainFont = font.load("gfx/marediv-regular.ttf", 64, font.filterNearest)
mainFont = font.load("gfx/marediv-regular.ttf", 64, font.Filter.nearest)
}

fn handle*() { }
Expand Down
2 changes: 1 addition & 1 deletion examples/space-shooter/menu.um
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ fn handle*() {
gui.box({
dimension: 10,
padding: 2,
dir: ui.BoxDirectionUp })
dir: ui.BoxDir.up })

if gui.qbutton("EXIT") {
global.scene = global.NONE
Expand Down
20 changes: 10 additions & 10 deletions examples/tetris/falling_piece.um
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ fn getKicks(from, to: int): [4]presets.Kick {
if clockwise {
return presets.ClockwiseI[from]
} else {
return presets.CounterclockwiseI[from]
return presets.CounterclockwiseI[from]
}
}
if clockwise {
return presets.ClockwiseNI[from]
}
return presets.CounterclockwiseNI[from]
return presets.CounterclockwiseNI[from]
}

fn checkTSpinCorners() {
Expand Down Expand Up @@ -88,14 +88,14 @@ fn runKickTable(table: [4]presets.Kick): bool {
if !invalid() {
if kind == presets.pieceTypeT {
isTspin = true
}
}
return true
}
x = original_x
y = original_y
}
return false
}
}

fn rotate180() {
from := rotation
Expand Down Expand Up @@ -180,18 +180,18 @@ fn update*(speed: real) {
placeTimer = 0
}

if (leftBtnLatch.pass(input.isPressed(input.key_left))) {

if (leftBtnLatch.pass(input.isPressed(input.Key.left))) {
placeTimer = 0
movePiece(-1, 0)
}

if (rightBtnLatch.pass(input.isPressed(input.key_right))) {
if (rightBtnLatch.pass(input.isPressed(input.Key.right))) {
placeTimer = 0
movePiece(1, 0)
}

if (input.isPressed(input.key_down)) {
if (input.isPressed(input.Key.down)) {
placeTimer += th.delta/1000.0
prev := y
y += 0.01*th.delta*speed;
Expand All @@ -200,7 +200,7 @@ fn update*(speed: real) {
}
}

if (input.isJustPressedc('X') || input.isJustPressed(input.key_up)) {
if (input.isJustPressedc('X') || input.isJustPressed(input.Key.up)) {
global.playSound(&global.audioRotate)
placeTimer = 0
rotateClockwise()
Expand All @@ -215,7 +215,7 @@ fn update*(speed: real) {
}
if (input.isJustPressedc(' ')) {
global.playSound(&global.audioPush)
push()
push()
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/tetris/gameplay.um
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn update*() {
field.init()
field.lost = false
}
if input.isJustPressed(input.key_shift) {
if input.isJustPressed(input.Key.shift) {
stashPiece()
}
if falling_piece.placeTimer > 0.5 || falling_piece.groundTouchTimer > 5 {
Expand Down
10 changes: 5 additions & 5 deletions main.um
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn onFrame(args: []any) {

p := th.Vf2{ 0, 1 }

if !input.isPressed(input.key_left_alt) {
if !input.isPressed(input.Key.leftAlt) {
for i in lines {
s := 1
if i == 1 {
Expand Down Expand Up @@ -56,10 +56,10 @@ fn onFrame(args: []any) {
}


if input.isJustPressed(input.key_enter) {
if th.platform == th.PlatformWindows {
if input.isJustPressed(input.Key.enter) {
if th.platform == th.Platform.windows {
std.system("explorer \"https://tophat2d.dev/\"")
} else if th.platform == th.PlatformLinux {
} else if th.platform == th.Platform.linux {
std.system("xdg-open https://tophat2d.dev/")
}
}
Expand All @@ -74,7 +74,7 @@ fn init*() {
window.setup("Hello world", 600, 600)
window.setViewport(th.Vf2{ 100, 100 })

loadedFont = font.load("etc/roboto.ttf", 16, 2)
loadedFont = font.load("etc/roboto.ttf", 16)
if !loadedFont.validate() {
exit(1, "could not load font")
}
Expand Down
32 changes: 28 additions & 4 deletions src/collisions.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// line to line is stolen from: http://jeffreythompson.org/collision-detection/table_of_contents.php
#include <float.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
Expand Down Expand Up @@ -42,14 +43,37 @@ th_point_to_quad(th_vf2 p, th_quad *q, th_vf2 *ic)
return coll;
}

static fu
point_distance(th_vf2 a, th_vf2 b)
{
a.x -= b.x;
a.y -= b.y;
return a.x * a.x + a.y * a.y;
}

uu
th_line_to_quad(th_vf2 b, th_vf2 e, th_quad *q, th_vf2 *ic)
{
for (uu i = 0; i < 4; i++)
if (th_line_to_line(b, e, q->v[i], q->v[(i + 1) % 4], ic))
return 1;
th_vf2 ic_c; // closest incident point
float ic_d = FLT_MAX; // incident distance
bool collision = false;

return 0;
for (uu i = 0; i < 4; i++) {
th_vf2 ic_n; // current incident point

if (th_line_to_line(b, e, q->v[i], q->v[(i + 1) % 4], &ic_n)) {
float ic_nd = point_distance(b, ic_n);
if (ic_nd < ic_d) {
ic_d = ic_nd;
ic_c = ic_n;
}
collision = true;
}
}

*ic = ic_c;

return collision;
}

uu
Expand Down
5 changes: 2 additions & 3 deletions src/raycast.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ th_ray_getcoll(
for (int i = 0; i < sceneLen && *collCount < maxColls; i++) {
th_quad q = th_ent_transform(scene[i]);

if (th_line_to_quad(ra->pos, p2, &q, &colls[*collCount].pos) ||
// in case the ray is completely inside the quad
th_point_to_quad(ra->pos, &q, &colls[*collCount].pos)) {
if (th_point_to_quad(ra->pos, &q, &colls[*collCount].pos) ||
th_line_to_quad(ra->pos, p2, &q, &colls[*collCount].pos)) {
++(*collCount);
}
}
Expand Down
Loading

0 comments on commit 77f0a66

Please sign in to comment.