summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/.playerObject.h.swpbin0 -> 12288 bytes
-rw-r--r--include/fixed.h77
-rw-r--r--include/playerObject.h73
-rw-r--r--include/point.h28
-rw-r--r--include/velocity.h35
5 files changed, 213 insertions, 0 deletions
diff --git a/include/.playerObject.h.swp b/include/.playerObject.h.swp
new file mode 100644
index 0000000..00a18a7
--- /dev/null
+++ b/include/.playerObject.h.swp
Binary files differ
diff --git a/include/fixed.h b/include/fixed.h
new file mode 100644
index 0000000..80d3ee9
--- /dev/null
+++ b/include/fixed.h
@@ -0,0 +1,77 @@
+#include "types.h"
+
+#ifndef FIXED_H
+#define FIXED_H
+
+#define FIX_SHIFT 8
+#define HALF_FIX_SHIFT 4
+#define FIX_SCALE ( 1 << FIX_SHIFT )
+#define FIX_SCALE_FLOAT ((float)(FIX_SCALE))
+
+typedef s32 FIXED;
+
+static inline FIXED fixed_OverMultiply(FIXED a, FIXED b) {
+ // This should multiply two fixed-point numbers sacrificing a little
+ // accuracy in exchange for less chance of an overflow
+
+ return ((a >> HALF_FIX_SHIFT) * (b >> HALF_FIX_SHIFT));
+}
+
+static inline FIXED fixed_multiply (FIXED a, FIXED b) {
+ // Multiply two fixed numbers. Possibility of overflow.
+ return (a * b) >> FIX_SHIFT;
+}
+
+static inline FIXED fixed_divide(FIXED a, FIXED b) {
+ // Divide two fixed point numbers
+ if (b != 0){
+ return (a * FIX_SCALE) / b;
+ }
+ else {
+ // Return a large number if division by zero
+ // Hopefully this won't break anything :P
+ return 100000 << FIX_SHIFT;
+ }
+}
+
+static inline FIXED float_to_fixed(float a) {
+ // Convert a float to fixed point
+ return ((FIXED)(a * FIX_SCALE_FLOAT));
+}
+
+static inline float fixed_to_float(FIXED a) {
+ // Convert fixed point to float
+ return (a / FIX_SCALE_FLOAT);
+}
+
+static inline void swapFixed(FIXED *a, FIXED *b) {
+ // Swap two fixed point integer pointers
+ FIXED temp;
+ temp = *a;
+ *a = *b;
+ *b = temp;
+}
+
+static inline FIXED fixed_sqrt(FIXED a, int iterations) {
+ // Calculate square root of a fixed-point number using Binary-Search
+ FIXED low = 0;
+ FIXED high = a;
+ FIXED mid;
+ FIXED midSquared;
+ for (int i = 0; i < iterations; i++) {
+ mid = fixed_divide((low + high), 2 << FIX_SHIFT);
+ midSquared = fixed_OverMultiply(mid, mid);
+ if (midSquared == a) {
+ return mid;
+ }
+ else if (midSquared > a) {
+ high = mid;
+ }
+ else {
+ low = mid;
+ }
+ }
+ return mid;
+}
+
+#endif // FIXED_H
diff --git a/include/playerObject.h b/include/playerObject.h
new file mode 100644
index 0000000..9394c4f
--- /dev/null
+++ b/include/playerObject.h
@@ -0,0 +1,73 @@
+#include "types.h"
+#include "point.h"
+#include "velocity.h"
+#include "toolbox.h"
+#include "fixed.h"
+
+#ifndef PLAYEROBJECT_H
+#define PLAYEROBJECT_H
+
+#define GRAVITY 0.75
+
+typedef struct playerObject {
+ VELOCITY vel;
+ POINT pt;
+ u32 rotation; // Rotation amount
+ OBJ_ATTR *obj; // Attributes of the object for GBA
+ int pallete_bank; // Index of pallete
+ int tile_id; // Index of player tile
+ int isJumping;
+} ALIGN(4) playerObject; // Word-align to ensure corruption doesn't occur
+
+static inline void initializePlayerObject (playerObject *object) {
+ // Initialize the point and velocity of an object
+ object->vel = createVelocity(0, 0);
+ object->pt = createPoint(20 << FIX_SHIFT, 100 << FIX_SHIFT);
+}
+
+static inline playerObject createPlayerObject (OBJ_ATTR *obj_buffer, int pallete_bank, int tile_id) {
+ // Create a player object from data
+ playerObject temp;
+ initializePlayerObject(&temp);
+ temp.rotation = 0;
+ temp.obj = obj_buffer;
+ temp.pallete_bank = pallete_bank;
+ temp.tile_id = tile_id;
+ temp.isJumping = 0;
+ obj_set_attr(temp.obj,
+ ATTR0_SQUARE, // Set attribute 1 to be a square
+ ATTR1_SIZE_16, // Set size to 16 x 16
+ ATTR2_PALBANK(pallete_bank) | tile_id // Which pallete to use, as we are in
+ // 16-color mode
+ );
+ obj_set_pos(temp.obj, temp.pt.x >> FIX_SHIFT, temp.pt.y >> FIX_SHIFT);
+ return temp;
+}
+
+static inline void applyGravity (playerObject *object) {
+ // Apply a gravity constant to a player
+ object->vel.dy += float_to_fixed(GRAVITY);
+ // This is a weird convention to have gravity in the positive direction,
+ // but I don't want to deal with coordinate changing. Too lazy
+}
+
+static inline void updatePlayer (playerObject *object, u32 GROUND_LEVEL) {
+ // Update the position of a player object
+ updatePoint(&object->pt, &object->vel);
+ if (object->pt.y >> FIX_SHIFT >= GROUND_LEVEL) {
+ // Only apply gravity if player is not touching the ground
+ object->isJumping = 0;
+ object->pt.y = GROUND_LEVEL << FIX_SHIFT; // Don't go through ground
+ object->vel.dy = 0;
+ }
+ else {
+ object->isJumping = 1;
+ applyGravity(object);
+ }
+ // Update the player object's attributes' position
+ obj_set_pos(object->obj, object->pt.x >> FIX_SHIFT, object->pt.y >> FIX_SHIFT);
+ // Update the player's second attribute (tile and pallete bank)
+ object->obj->attr2 = ATTR2_BUILD(object->tile_id, object->pallete_bank, 0);
+}
+
+#endif // PLAYEROBJECT_H
diff --git a/include/point.h b/include/point.h
new file mode 100644
index 0000000..5c69422
--- /dev/null
+++ b/include/point.h
@@ -0,0 +1,28 @@
+#include "types.h"
+#include "fixed.h"
+
+#ifndef POINT_H
+#define POINT_H
+
+typedef struct POINT {
+ FIXED x;
+ FIXED y;
+} ALIGN(4) POINT;
+
+static inline POINT createPoint (FIXED x, FIXED y) {
+ // Create a point from data
+ POINT temp;
+ temp.x = x;
+ temp.y = y;
+ return temp;
+}
+
+static inline POINT addPoint (POINT *a, POINT *b) {
+ // Add two points
+ POINT temp;
+ temp.x = a->x + b->x;
+ temp.y = a->y + b->y;
+ return temp;
+}
+
+#endif // POINT_H
diff --git a/include/velocity.h b/include/velocity.h
new file mode 100644
index 0000000..278226c
--- /dev/null
+++ b/include/velocity.h
@@ -0,0 +1,35 @@
+#include "types.h"
+#include "fixed.h"
+#include "point.h"
+
+#ifndef VELOCITY_H
+#define VELOCITY_H
+
+typedef struct VELOCITY {
+ FIXED dx;
+ FIXED dy;
+} ALIGN(4) VELOCITY;
+
+static inline VELOCITY createVelocity (FIXED dx, FIXED dy) {
+ // Create velocity from data
+ VELOCITY temp;
+ temp.dx = dx;
+ temp.dy = dy;
+ return temp;
+}
+
+static inline VELOCITY addVelocities (VELOCITY *a, VELOCITY *b) {
+ // Add two velocities
+ VELOCITY temp;
+ temp.dx = a->dx + b->dx;
+ temp.dy = a->dy + b->dy;
+ return temp;
+}
+
+static inline void updatePoint (POINT *pt, VELOCITY *vel) {
+ // Update a point with a velocity
+ pt->x += vel->dx;
+ pt->y += vel->dy;
+}
+
+#endif // VELOCITY_H