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#pragma once
24
25#include "retro_endianness.h"
26#include "tic80_config.h"
27#include "tic80_types.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
33#define TIC80_WIDTH 240
34#define TIC80_HEIGHT 136
35#define TIC80_FULLWIDTH_BITS 8
36#define TIC80_FULLWIDTH (1 << TIC80_FULLWIDTH_BITS)
37#define TIC80_FULLHEIGHT (TIC80_FULLWIDTH*9/16)
38
39#define TIC80_MARGIN_TOP ((TIC80_FULLHEIGHT - TIC80_HEIGHT) / 2)
40#define TIC80_MARGIN_BOTTOM TIC80_MARGIN_TOP
41#define TIC80_MARGIN_LEFT ((TIC80_FULLWIDTH - TIC80_WIDTH) / 2)
42#define TIC80_MARGIN_RIGHT TIC80_MARGIN_LEFT
43
44#define TIC80_KEY_BUFFER 4
45#define TIC80_SAMPLERATE 44100
46#define TIC80_SAMPLETYPE s16
47#define TIC80_SAMPLESIZE sizeof(TIC80_SAMPLETYPE)
48#define TIC80_SAMPLE_CHANNELS 2
49#define TIC80_FRAMERATE 60
50
51typedef enum {
52 TIC80_PIXEL_COLOR_ARGB8888 = (1 << 8) | 32,
53 TIC80_PIXEL_COLOR_ABGR8888 = (2 << 8) | 32,
54 TIC80_PIXEL_COLOR_RGBA8888 = (3 << 8) | 32,
55 TIC80_PIXEL_COLOR_BGRA8888 = (4 << 8) | 32
56} tic80_pixel_color_format;
57
58typedef struct
59{
60 struct
61 {
62 void (*trace)(const char* text, u8 color);
63 void (*error)(const char* info);
64 void (*exit)();
65 } callback;
66
67 struct
68 {
69 TIC80_SAMPLETYPE* buffer;
70 s32 count;
71 } samples;
72
73 u32 *screen;
74} tic80;
75
76typedef union
77{
78 struct
79 {
80#if RETRO_IS_BIG_ENDIAN
81 u8 y:1;
82 u8 x:1;
83 u8 b:1;
84 u8 a:1;
85 u8 right:1;
86 u8 left:1;
87 u8 down:1;
88 u8 up:1;
89#else
90 u8 up:1;
91 u8 down:1;
92 u8 left:1;
93 u8 right:1;
94 u8 a:1;
95 u8 b:1;
96 u8 x:1;
97 u8 y:1;
98#endif
99 };
100
101 u8 data;
102} tic80_gamepad;
103
104typedef union
105{
106 struct
107 {
108#if RETRO_IS_BIG_ENDIAN
109 tic80_gamepad fourth;
110 tic80_gamepad third;
111 tic80_gamepad second;
112 tic80_gamepad first;
113#else
114 tic80_gamepad first;
115 tic80_gamepad second;
116 tic80_gamepad third;
117 tic80_gamepad fourth;
118#endif
119 };
120
121 u32 data;
122} tic80_gamepads;
123
124typedef struct
125{
126 union
127 {
128 // absolute pos
129 struct
130 {
131 u8 x;
132 u8 y;
133 };
134
135 // releative values
136 struct
137 {
138 s8 rx;
139 s8 ry;
140 };
141 };
142
143 union
144 {
145 struct
146 {
147 u16 left:1;
148 u16 middle:1;
149 u16 right:1;
150
151 s16 scrollx:6;
152 s16 scrolly:6;
153
154 u16 relative:1;
155 };
156
157 u16 btns;
158 };
159} tic80_mouse;
160
161typedef u8 tic_key;
162
163typedef union
164{
165 tic_key keys[TIC80_KEY_BUFFER];
166 u32 data;
167} tic80_keyboard;
168
169typedef struct
170{
171 tic80_gamepads gamepads;
172 tic80_mouse mouse;
173 tic80_keyboard keyboard;
174
175} tic80_input;
176
177TIC80_API tic80* tic80_create(s32 samplerate, tic80_pixel_color_format format);
178TIC80_API void tic80_load(tic80* tic, void* cart, s32 size);
179TIC80_API void tic80_tick(tic80* tic, tic80_input input);
180TIC80_API void tic80_sound(tic80* tic);
181TIC80_API void tic80_delete(tic80* tic);
182
183#ifdef __cplusplus
184}
185#endif
186