this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #2227 from Gota7/main

C API accuracy improvements and better layout

authored by

Vadim Grigoruk and committed by
GitHub
48d8a584 0cc85254

+208 -113
+1 -1
templates/c/src/main.c
··· 10 10 int t, x, y; 11 11 const char* m = "HELLO WORLD FROM C!"; 12 12 int r = 0; 13 - MouseData md; 13 + Mouse md; 14 14 uint8_t transcolors = { 14 }; 15 15 16 16 WASM_EXPORT("BOOT")
+207 -112
templates/c/src/tic80.h
··· 6 6 #define WASM_EXPORT(name) __attribute__((export_name(name))) 7 7 #define WASM_IMPORT(name) __attribute__((import_name(name))) 8 8 9 + // --------------------------- 10 + // Screen 11 + // --------------------------- 12 + 13 + // How big each tile is in pixels. 14 + #define TILE_SIZE 8 15 + 16 + // How many pixels wide the screen is. 17 + #define WIDTH 240 18 + 19 + // How many pixels tall the screen is. 20 + #define HEIGHT 136 21 + 22 + // How many tiles wide the screen is. 23 + #define WIDTH_TILES WIDTH / TILE_SIZE 24 + 25 + // How many tiles tall the screen is. 26 + #define HEIGHT_TILES HEIGHT / TILE_SIZE 27 + 28 + // How many bits-per-pixel. 29 + #define BPP 4 30 + 31 + // --------------------------- 32 + // Structures 33 + // --------------------------- 34 + 35 + // Keyboard key codes. 9 36 enum KEYCODES { 10 37 KEY_NULL, 11 38 KEY_A, ··· 75 102 KEY_ALT 76 103 }; 77 104 105 + // Gamepad button codes. 78 106 enum BUTTON_CODES 79 107 { 80 108 BUTTON_CODE_P1_UP, ··· 111 139 BUTTON_CODE_P4_Y 112 140 }; 113 141 142 + // Video RAM. 114 143 typedef struct { 115 - uint8_t SCREEN[16320]; 116 - uint8_t PALETTE[48]; 117 - uint8_t PALETTE_MAP[8]; 118 - uint8_t BORDER_COLOR; 119 - uint8_t SCREEN_OFFSET_X; 120 - uint8_t SCREEN_OFFSET_Y; 121 - uint8_t MOUSE_CURSOR; 144 + uint8_t SCREEN[WIDTH * HEIGHT * BPP / 8]; 145 + uint8_t PALETTE[48]; // 16 colors. 146 + uint8_t PALETTE_MAP[8]; // 16 indices. 147 + uint8_t BORDER_COLOR_AND_OVR_TRANSPARENCY; // Bank 0 is border color, bank 1 is OVR transparency. 148 + int8_t SCREEN_OFFSET_X; 149 + int8_t SCREEN_OFFSET_Y; 150 + int8_t MOUSE_CURSOR; 122 151 uint8_t BLIT_SEGMENT; 123 152 uint8_t RESERVED[3]; 124 153 } VRAM; 125 154 155 + // Mouse data. 126 156 typedef struct { 127 - short x; short y; 128 - char scrollx; char scrolly; 157 + int16_t x; int16_t y; 158 + int8_t scrollx; int8_t scrolly; 129 159 bool left; bool middle; bool right; 130 - } MouseData; 131 - 132 - #define TILE_SIZE 8 133 - #define WIDTH 240 134 - #define HEIGHT 136 160 + } Mouse; 135 161 136 - // Constants. 137 - const uint32_t TILES_SIZE = 8192; 138 - const uint32_t SPRITES_SIZE = 8192; 139 - const uint32_t MAP_SIZE = 32640; 140 - const uint32_t GAMEPADS_SIZE = 4; 141 - const uint32_t MOUSE_SIZE = 4; 142 - const uint32_t KEYBOARD_SIZE = 4; 143 - const uint32_t SFX_STATE_SIZE = 16; 144 - const uint32_t SOUND_REGISTERS_SIZE = 72; 145 - const uint32_t WAVEFORMS_SIZE = 256; 146 - const uint32_t SFX_SIZE = 4224; 147 - const uint32_t MUSIC_PATTERNS_SIZE = 11520; 148 - const uint32_t MUSIC_TRACKS_SIZE = 408; 149 - const uint32_t SOUND_STATE_SIZE = 4; 150 - const uint32_t STEREO_VOLUME_SIZE = 4; 151 - const uint32_t PERSISTENT_MEMORY_SIZE = 1024; 152 - const uint32_t SPRITE_FLAGS_SIZE = 512; 153 - const uint32_t SYSTEM_FONT_SIZE = 2048; 154 - const uint32_t WASM_FREE_RAM_SIZE = 163840; // 160kb 162 + // --------------------------- 163 + // Pointers 164 + // --------------------------- 155 165 156 - // Pointers. 157 166 VRAM* FRAMEBUFFER = (VRAM*)0; 158 167 uint8_t* TILES = (uint8_t*)0x04000; 159 168 uint8_t* SPRITES = (uint8_t*)0x06000; ··· 174 183 uint8_t* SYSTEM_FONT = (uint8_t*)0x14604; 175 184 uint8_t* WASM_FREE_RAM = (uint8_t*)0x18000; // 160kb 176 185 177 - // Functions. 178 - WASM_IMPORT("btn") 179 - int32_t btn(int32_t id); 186 + // --------------------------- 187 + // Constants 188 + // --------------------------- 180 189 181 - WASM_IMPORT("btnp") 182 - bool btnp(int32_t id, int32_t hold, int32_t period); 190 + const uint32_t TILES_SIZE = 0x2000; 191 + const uint32_t SPRITES_SIZE = 0x2000; 192 + const uint32_t MAP_SIZE = 32640; 193 + const uint32_t GAMEPADS_SIZE = 4; 194 + const uint32_t MOUSE_SIZE = 4; 195 + const uint32_t KEYBOARD_SIZE = 4; 196 + const uint32_t SFX_STATE_SIZE = 16; 197 + const uint32_t SOUND_REGISTERS_SIZE = 72; 198 + const uint32_t WAVEFORMS_SIZE = 256; 199 + const uint32_t SFX_SIZE = 4224; 200 + const uint32_t MUSIC_PATTERNS_SIZE = 11520; 201 + const uint32_t MUSIC_TRACKS_SIZE = 408; 202 + const uint32_t SOUND_STATE_SIZE = 4; 203 + const uint32_t STEREO_VOLUME_SIZE = 4; 204 + const uint32_t PERSISTENT_MEMORY_SIZE = 1024; 205 + const uint32_t SPRITE_FLAGS_SIZE = 512; 206 + const uint32_t SYSTEM_FONT_SIZE = 2048; 207 + const uint32_t WASM_FREE_RAM_SIZE = 163840; // 160kb 208 + 209 + // --------------------------- 210 + // Drawing Functions 211 + // --------------------------- 183 212 184 213 WASM_IMPORT("circ") 185 - void circ(int32_t x, int32_t y, int32_t radius, int32_t color); 214 + // Draw a filled circle. 215 + void circ(int32_t x, int32_t y, int32_t radius, int8_t color); 186 216 187 217 WASM_IMPORT("circb") 188 - void circb(int32_t x, int32_t y, int32_t radius, int32_t color); 218 + // Draw a circle border. 219 + void circb(int32_t x, int32_t y, int32_t radius, int8_t color); 220 + 221 + WASM_IMPORT("elli") 222 + // Draw a filled ellipse. 223 + void elli(int32_t x, int32_t y, int32_t a, int32_t b, int8_t color); 224 + 225 + WASM_IMPORT("ellib") 226 + // Draw an ellipse border. 227 + void ellib(int32_t x, int32_t y, int32_t a, int32_t b, int8_t color); 189 228 190 229 WASM_IMPORT("clip") 191 - void clip(int32_t x, int32_t y, int32_t w, int32_t h); 230 + // Set the screen clipping region. 231 + void clip(int32_t x, int32_t y, int32_t width, int32_t height); 192 232 193 233 WASM_IMPORT("cls") 194 - void cls(int32_t color); 234 + // Clear the screen. 235 + void cls(int8_t color); 195 236 196 - WASM_IMPORT("exit") 197 - void exit(); 237 + WASM_IMPORT("font") 238 + // Print a string using foreground sprite data as the font. 239 + int8_t font(const char* text, int32_t x, int32_t y, uint8_t* trans_colors, int8_t trans_count, int8_t char_width, int8_t char_height, bool fixed, int8_t scale, bool alt); 198 240 199 - WASM_IMPORT("elli") 200 - void elli(int32_t x, int32_t y, int32_t a, int32_t b, int32_t color); 241 + WASM_IMPORT("line") 242 + // Draw a straight line. 243 + void line(float x0, float y0, float x1, float y1, int8_t color); 201 244 202 - WASM_IMPORT("ellib") 203 - void ellib(int32_t x, int32_t y, int32_t a, int32_t b, int32_t color); 245 + WASM_IMPORT("map") 246 + // Draw a map region. 247 + void map(int32_t x, int32_t y, int32_t w, int32_t h, int32_t sx, int32_t sy, uint8_t* trans_colors, int8_t colorCount, int8_t scale, int32_t remap); 248 + 249 + WASM_IMPORT("pix") 250 + // Get or set the color of a single pixel. 251 + uint8_t pix(int32_t x, int32_t y, int8_t color); 252 + 253 + WASM_IMPORT("print") 254 + // Print a string using the system font. 255 + int32_t print(const char* text, int32_t x, int32_t y, int8_t color, int8_t fixed, int32_t scale, int8_t alt); 256 + 257 + WASM_IMPORT("rect") 258 + // Draw a filled rectangle. 259 + void rect(int32_t x, int32_t y, int32_t w, int32_t h, int8_t color); 260 + 261 + WASM_IMPORT("rectb") 262 + // Draw a rectangle border. 263 + void rectb(int32_t x, int32_t y, int32_t w, int32_t h, int8_t color); 264 + 265 + WASM_IMPORT("spr") 266 + // Draw a sprite or composite sprite. 267 + void spr(int32_t id, int32_t x, int32_t y, uint8_t* trans_colors, int8_t color_count, int32_t scale, int32_t flip, int32_t rotate, int32_t w, int32_t h); 204 268 205 - WASM_IMPORT("fget") 206 - bool fget(int32_t id, uint8_t flag); 269 + WASM_IMPORT("tri") 270 + // Draw a filled triangle. 271 + void tri(float x1, float y1, float x2, float y2, float x3, float y3, int8_t color); 207 272 208 - WASM_IMPORT("font") 209 - int32_t font(const char* text, int32_t x, int32_t y, uint32_t* transcolors, int32_t colorcount, int32_t width, int32_t height, bool fixed, int32_t scale, bool alt); 273 + WASM_IMPORT("trib") 274 + // Draw a triangle border. 275 + void trib(float x1, float y1, float x2, float y2, float x3, float y3, int8_t color); 210 276 211 - WASM_IMPORT("fset") 212 - bool fset(int32_t id, uint8_t flag, bool value); 277 + WASM_IMPORT("ttri") 278 + // Draw a triangle filled with texture. 279 + void ttri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int32_t texsrc, uint8_t* trans_colors, int8_t color_count, float z1, float z2, float z3, bool depth); 213 280 214 - WASM_IMPORT("key") 215 - bool key(int32_t keycode); 281 + // --------------------------- 282 + // Input Functions 283 + // --------------------------- 216 284 217 - WASM_IMPORT("keyp") 218 - bool keyp(int32_t keycode, int32_t hold, int32_t period); 285 + WASM_IMPORT("btn") 286 + // Get gamepad button state in current frame. 287 + int32_t btn(int32_t index); 219 288 220 - WASM_IMPORT("line") 221 - void line(float x0, float y0, float x1, float y1, int8_t color); 289 + WASM_IMPORT("btnp") 290 + // Get gamepad button state according to previous frame. 291 + bool btnp(int32_t index, int32_t hold, int32_t period); 222 292 223 - WASM_IMPORT("map") 224 - void map(int32_t x, int32_t y, int32_t w, int32_t h, int32_t sx, int32_t sy, uint32_t* transcolors, int32_t colorcount, int32_t scale, int32_t remap); 293 + WASM_IMPORT("key") 294 + // Get keyboard button state in current frame. 295 + int32_t key(int32_t x); 225 296 226 - WASM_IMPORT("mget") 227 - int32_t mget(int32_t x, int32_t y); 297 + WASM_IMPORT("keyp") 298 + // Get keyboard button state relative to previous frame. 299 + int32_t keyp(int8_t x, int32_t hold, int32_t period); 228 300 229 301 WASM_IMPORT("mouse") 230 - void mouse(MouseData* data); 302 + // Get XY and press state of mouse/touch. 303 + void mouse(Mouse* mouse_ptr_addy); 231 304 232 - WASM_IMPORT("mset") 233 - void mset(int32_t x, int32_t y, int32_t value); 305 + // --------------------------- 306 + // Sound Functions 307 + // --------------------------- 234 308 235 309 WASM_IMPORT("music") 310 + // Play or stop playing music. 236 311 void music(int32_t track, int32_t frame, int32_t row, bool loop, bool sustain, int32_t tempo, int32_t speed); 237 312 238 - WASM_IMPORT("peek") 239 - uint8_t peek(int32_t addr, int32_t bits); 313 + WASM_IMPORT("sfx") 314 + // Play or stop playing a given sound. 315 + void sfx(int32_t sfx_id, int32_t note, int32_t octave, int32_t duration, int32_t channel, int32_t volume_left, int32_t volume_right, int32_t speed); 240 316 241 - WASM_IMPORT("peek4") 242 - uint8_t peek4(uint32_t addr4); 317 + // --------------------------- 318 + // Memory Functions 319 + // --------------------------- 243 320 244 - WASM_IMPORT("peek2") 245 - uint8_t peek2(uint32_t addr2); 321 + WASM_IMPORT("pmem") 322 + // Access or update the persistent memory. 323 + uint32_t pmem(int32_t address, int64_t value); 324 + 325 + WASM_IMPORT("peek") 326 + // Read a byte from an address in RAM. 327 + int8_t peek(int32_t address, int8_t bits); 246 328 247 329 WASM_IMPORT("peek1") 248 - uint8_t peek1(uint32_t bitaddr); 330 + // Read a single bit from an address in RAM. 331 + int8_t peek1(int32_t address); 249 332 250 - WASM_IMPORT("pix") 251 - void pix(int32_t x, int32_t y, int32_t color); 333 + WASM_IMPORT("peek2") 334 + // Read two bit value from an address in RAM. 335 + int8_t peek2(int32_t address); 252 336 253 - WASM_IMPORT("pmem") 254 - uint32_t pmem(uint32_t index, uint32_t value); 337 + WASM_IMPORT("peek4") 338 + // Read a nibble value from an address. 339 + int8_t peek4(int32_t address); 255 340 256 341 WASM_IMPORT("poke") 257 - void poke(int32_t addr, int8_t value, int8_t bits); 342 + // Write a byte value to an address in RAM. 343 + void poke(int32_t address, int8_t value, int8_t bits); 258 344 259 - WASM_IMPORT("poke4") 260 - void poke4(int32_t addr4, int8_t value); 345 + WASM_IMPORT("poke1") 346 + // Write a single bit to an address in RAM. 347 + void poke1(int32_t address, int8_t value); 261 348 262 349 WASM_IMPORT("poke2") 263 - void poke2(int32_t addr2, int8_t value); 350 + // Write a two bit value to an address in RAM. 351 + void poke2(int32_t address, int8_t value); 264 352 265 - WASM_IMPORT("poke1") 266 - void poke1(int32_t bitaddr, int8_t value); 353 + WASM_IMPORT("poke4") 354 + // Write a nibble value to an address in RAM. 355 + void poke4(int32_t address, int8_t value); 267 356 268 - WASM_IMPORT("print") 269 - int32_t print(const char* txt, int32_t x, int32_t y, int32_t color, int32_t fixed, int32_t scale, int32_t alt); 357 + WASM_IMPORT("sync") 358 + // Copy banks of RAM (sprites, map, etc) to and from the cartridge. 359 + void sync(int32_t mask, int8_t bank, int8_t to_cart); 270 360 271 - WASM_IMPORT("rect") 272 - void rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t color); 361 + WASM_IMPORT("vbank") 362 + // Switch the 16kb of banked video RAM. 363 + int8_t vbank(int8_t bank); 273 364 274 - WASM_IMPORT("rectb") 275 - void rectb(int32_t x, int32_t y, int32_t w, int32_t h, int32_t color); 365 + // --------------------------- 366 + // Utility Functions 367 + // --------------------------- 276 368 277 - WASM_IMPORT("reset") 278 - void reset(); 369 + WASM_IMPORT("fget") 370 + // Retrieve a sprite flag. 371 + bool fget(int32_t sprite_index, int8_t flag); 279 372 280 - WASM_IMPORT("sfx") 281 - void sfx(int32_t id, int32_t note, int32_t octave, int32_t duration, int32_t channel, int32_t volumeLeft, int32_t volumeRight, int32_t speed); 373 + WASM_IMPORT("fset") 374 + // Update a sprite flag. 375 + bool fset(int32_t sprite_index, int8_t flag, bool value); 282 376 283 - WASM_IMPORT("spr") 284 - void spr(int32_t id, int32_t x, int32_t y, uint8_t* transcolors, uint32_t colorcount, int32_t scale, int32_t flip, int32_t rotate, int32_t w, int32_t h); 377 + WASM_IMPORT("mget") 378 + // Retrieve a map tile at given coordinates. 379 + int32_t mget(int32_t x, int32_t y); 285 380 286 - WASM_IMPORT("sync") 287 - void sync(int32_t mask, int32_t bank, bool tocart); 381 + WASM_IMPORT("mset") 382 + // Update a map tile at given coordinates. 383 + void mset(int32_t x, int32_t y, int32_t value); 288 384 289 - WASM_IMPORT("trace") 290 - void trace(const char* txt, int32_t color); 385 + // --------------------------- 386 + // System Functions 387 + // --------------------------- 291 388 292 - WASM_IMPORT("ttri") 293 - void ttri(float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, int32_t texsrc, uint32_t* transcolors, int32_t colorcount, float z1, float z2, float z3, bool persp); 294 - 295 - WASM_IMPORT("tri") 296 - void tri(float x1, float y1, float x2, float y2, float x3, float y3, int32_t color); 297 - 298 - WASM_IMPORT("trib") 299 - void trib(float x1, float y1, float x2, float y2, float x3, float y3, int32_t color); 389 + WASM_IMPORT("exit") 390 + // Interrupt program and return to console. 391 + void exit(); 300 392 301 393 WASM_IMPORT("time") 394 + // Returns how many milliseconds have passed since game started. 302 395 float time(); 303 396 304 397 WASM_IMPORT("tstamp") 305 - int32_t tstamp(); 398 + // Returns the current Unix timestamp in seconds. 399 + uint32_t tstamp(); 306 400 307 - WASM_IMPORT("vbank") 308 - int32_t vbank(int32_t bank); 401 + WASM_IMPORT("trace") 402 + // Print a string to the Console. 403 + void trace(const char* text, int8_t color);