-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
152 lines (124 loc) · 4.28 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//SPDX-License-Identifier: BSD-3-Clause
//SPDX-FileCopyrightText: 2020 Lorenzo Cauli (lorecast162)
//XBOX Defines
#include <hal/debug.h>
#include <hal/video.h>
#include <hal/xbox.h>
#include <windows.h>
//SDL Defines
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_gamecontroller.h>
static void printSDLErrorAndReboot(void);
static void printIMGErrorAndReboot(void);
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define FPS 60
void game(void){
//fix to make joystick work
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
//declare SDL window, event and renderer
SDL_Window* window = NULL;
SDL_Event event;
SDL_Renderer* renderer = NULL;
//init SDL video and game controller
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't init SDL.\n");
printSDLErrorAndReboot();
}
//create window
window = SDL_CreateWindow("rsbsPLUS-xboxen",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
//throw error if window wasn't created
if (window == NULL) {
debugPrint("Window could not be created.\n");
SDL_VideoQuit();
printSDLErrorAndReboot();
}
//create renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
//init SDL_image with jpg
if (!(IMG_Init(IMG_INIT_JPG) & IMG_INIT_JPG)){
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't init SDL_image.\n");
printSDLErrorAndReboot();
}
//load up circle textures
SDL_Texture* blauTexture = IMG_LoadTexture(renderer, "D:\\res\\blau.bmp");
SDL_Texture* rotTexture = IMG_LoadTexture(renderer, "D:\\res\\rot.bmp");
SDL_Texture* gruenTexture = IMG_LoadTexture(renderer, "D:\\res\\gruen.bmp");
//load bg texture
SDL_Texture* bgTexture = IMG_LoadTexture(renderer, "D:\\res\\bg.bmp");
//create rects for circles and the screen. used in rendering
SDL_Rect sRect = {256,176,128,128};
SDL_Rect screenRect = {0,0,SCREEN_WIDTH,SCREEN_HEIGHT};
//log how many joysticks were found
debugPrint("%i joysticks were found.\n\n", SDL_NumJoysticks() );
//create joystick and open joystick 0
SDL_GameController* joystick;
SDL_GameControllerEventState(SDL_ENABLE);
joystick = SDL_GameControllerOpen(0);
//set current circle to default circle (red)
SDL_Texture* curCircle = rotTexture;
//declare game vars
uint8_t moveDelta = 10;
//declare variable to stop game loop
char done = 0;
while (!done) {
if (SDL_GetTicks() < 1000 / FPS) {
SDL_Delay((1000 / FPS) - SDL_GetTicks());
}
//wait for vblank
XVideoWaitForVBlank();
//event loop
while (SDL_PollEvent(&event)) {
//check event type
switch (event.type) {
case SDL_QUIT:
done = 1;
break;
}
}
//if right is pressed and resulting x is not out of the screen make circle go right
if (SDL_GameControllerGetButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) && (sRect.x + moveDelta <= SCREEN_WIDTH - 128 ) ) sRect.x += moveDelta;
//if left is set and resulting x is not out of the screen make circle go left
else if (SDL_GameControllerGetButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_LEFT) && (sRect.x > 0) ) sRect.x -= moveDelta;
//if up is pressed set to blue
if (SDL_GameControllerGetButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_UP)) curCircle = blauTexture;
//otherwise if down is pressed set to green
else if (SDL_GameControllerGetButton(joystick, SDL_CONTROLLER_BUTTON_DPAD_DOWN)) curCircle = gruenTexture;
//otherwise set to red
else curCircle = rotTexture;
//render out background to clear screen
SDL_RenderCopy(renderer, bgTexture, NULL, &screenRect);
//render circle
SDL_RenderCopy(renderer, curCircle, NULL, &sRect);
//"blit" render to window
SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(window);
}
//stop video system
SDL_VideoQuit();
//close joystick stream
SDL_GameControllerClose(joystick);
}
int main() {
//set video mode
XVideoSetMode(SCREEN_WIDTH,SCREEN_HEIGHT,32,REFRESH_DEFAULT);
game();
return 0;
}
//functions for error reporting
static void printSDLErrorAndReboot(void) {
debugPrint("SDL_Error: %s\n", SDL_GetError());
debugPrint("Rebooting in 5 seconds.\n");
Sleep(5000);
XReboot();
}
static void printIMGErrorAndReboot(void) {
debugPrint("SDL_Image Error: %s\n", IMG_GetError());
debugPrint("Rebooting in 5 seconds.\n");
Sleep(5000);
XReboot();
}