| 1 | // MIT License |
| 2 | |
| 3 | // Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com |
| 4 | |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | // of this software and associated documentation files (the "Software"), to deal |
| 7 | // in the Software without restriction, including without limitation the rights |
| 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | // copies of the Software, and to permit persons to whom the Software is |
| 10 | // furnished to do so, subject to the following conditions: |
| 11 | |
| 12 | // The above copyright notice and this permission notice shall be included in all |
| 13 | // copies or substantial portions of the Software. |
| 14 | |
| 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | // SOFTWARE. |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <tic80.h> |
| 27 | #include "api.h" |
| 28 | #include "tools.h" |
| 29 | #include "cart.h" |
| 30 | |
| 31 | static void onTrace(void* data, const char* text, u8 color) |
| 32 | { |
| 33 | tic80* tic = (tic80*)data; |
| 34 | |
| 35 | if(tic->callback.trace) |
| 36 | tic->callback.trace(text, color); |
| 37 | } |
| 38 | |
| 39 | static void onError(void* data, const char* info) |
| 40 | { |
| 41 | tic80* tic = (tic80*)data; |
| 42 | |
| 43 | if(tic->callback.error) |
| 44 | tic->callback.error(info); |
| 45 | } |
| 46 | |
| 47 | static void onExit(void* data) |
| 48 | { |
| 49 | tic80* tic = (tic80*)data; |
| 50 | |
| 51 | if(tic->callback.exit) |
| 52 | tic->callback.exit(); |
| 53 | } |
| 54 | |
| 55 | tic80* tic80_create(s32 samplerate, tic80_pixel_color_format format) |
| 56 | { |
| 57 | return &tic_core_create(samplerate, format)->product; |
| 58 | } |
| 59 | |
| 60 | TIC80_API void tic80_load(tic80* tic, void* cart, s32 size) |
| 61 | { |
| 62 | tic_mem* mem = (tic_mem*)tic; |
| 63 | |
| 64 | tic_cart_load(&mem->cart, cart, size); |
| 65 | tic_api_reset(mem); |
| 66 | } |
| 67 | |
| 68 | TIC80_API void tic80_tick(tic80* tic, tic80_input input) |
| 69 | { |
| 70 | tic_mem* mem = (tic_mem*)tic; |
| 71 | |
| 72 | mem->ram->input = input; |
| 73 | |
| 74 | tic_tick_data tickData = (tic_tick_data) |
| 75 | { |
| 76 | .error = onError, |
| 77 | .trace = onTrace, |
| 78 | .exit = onExit, |
| 79 | .data = tic, |
| 80 | }; |
| 81 | |
| 82 | tic_core_tick_start(mem); |
| 83 | tic_core_tick(mem, &tickData); |
| 84 | tic_core_tick_end(mem); |
| 85 | |
| 86 | tic_core_blit(mem); |
| 87 | } |
| 88 | |
| 89 | TIC80_API void tic80_sound(tic80* tic) |
| 90 | { |
| 91 | tic_mem* mem = (tic_mem*)tic; |
| 92 | tic_core_synth_sound(mem); |
| 93 | } |
| 94 | |
| 95 | TIC80_API void tic80_delete(tic80* tic) |
| 96 | { |
| 97 | tic_mem* mem = (tic_mem*)tic; |
| 98 | tic_core_close(mem); |
| 99 | } |
| 100 | |