-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
216 lines (187 loc) · 4.81 KB
/
main.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
#include "jloader.h"
#include "controller.h"
using namespace std;
SDL_Window* gWindow = NULL;
//Reshape the JStruct data into a 5x5 matrix of ints.
void To2DMat( JStruct * js, int** arr, int x, int y){
int index = 0;
for(int i = 0; i < 5; i++){
for(int j = 0; j < 5; j++){
int val = js -> jData[index];
arr[i][j] = val;
index++;
}
}
}
int main ( int argc, char** argv )
{
int* jdll = (int *)malloc(sizeof(int*));
int jInstance = 0;
JStruct * jStruct = (JStruct *)malloc(sizeof(JStruct));
int** pt = (int**) malloc(sizeof(int*)*4);
int ctr = 0;
for(ctr=0;ctr<4;ctr++)
{
pt[ctr]=(int *)malloc(sizeof(int));
}
jInstance = setupJInstance(jdll, jInstance);
setupArr(jdll, jInstance);
getArr(jdll, jInstance, jStruct,pt);
int s = 5;
int t = 5;
int ** grid = (int**)malloc(s * sizeof(int*));
for(int i = 0; i < s; i++){
grid[i] = (int*)malloc(t* sizeof(int));
}
To2DMat(jStruct, grid, t, s);
// the x and y pointers - which rows / columns to rotate
int xPointer = 0;
int yPointer = 0;
//drawing rect
SDL_Rect dstrect;
// initialize SDL video
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
gWindow = SDL_CreateWindow( "Demo J Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1000, 600, SDL_WINDOW_SHOWN );
SDL_Surface* screen = NULL;
screen = SDL_GetWindowSurface( gWindow );
if ( !screen )
{
printf("Unable to set video: %s\n", SDL_GetError());
return 1;
}
//try to load the images.
SDL_Surface* r;
SDL_Surface* w;
SDL_Surface* b;
SDL_Surface* y;
SDL_Surface* g;
r = IMG_Load("images/red.png");
w = IMG_Load("images/white.png");
b = IMG_Load("images/blue.png");
y = IMG_Load("images/yellow.png");
g = IMG_Load("images/green.png");
if (!r || !w || !b || !y || !g)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// program main loop
bool done = false;
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
// Rotate horizontrally or vertically, using arrow keys
else if (event.key.keysym.sym == SDLK_RIGHT){
rotateRow(jdll, jInstance, yPointer,-1);
getArr(jdll, jInstance, jStruct,pt);
To2DMat(jStruct, grid, t, s);
}
else if (event.key.keysym.sym == SDLK_LEFT){
rotateRow(jdll, jInstance, yPointer,1);
getArr(jdll, jInstance, jStruct,pt);
To2DMat(jStruct, grid, t, s);
}
else if (event.key.keysym.sym == SDLK_DOWN){
rotateColumn(jdll, jInstance, xPointer,-1);
getArr(jdll, jInstance, jStruct,pt);
To2DMat(jStruct, grid, t, s);
}
else if (event.key.keysym.sym == SDLK_UP){
rotateColumn(jdll, jInstance, xPointer,1);
getArr(jdll, jInstance, jStruct,pt);
To2DMat(jStruct, grid, t, s);
}
// move the horizontal / vertical pointers using w,s,a,d keys.
else if (event.key.keysym.sym == SDLK_w){
yPointer = (yPointer - 1 + 5) % 5;
}
else if (event.key.keysym.sym == SDLK_s){
yPointer = (yPointer + 1 + 5) % 5;
}
else if (event.key.keysym.sym == SDLK_a){
xPointer = (xPointer - 1 + 5) % 5;
}
else if (event.key.keysym.sym == SDLK_d){
xPointer = (xPointer + 1 + 5) % 5;
}
break;
}
}
}
// DRAWING STARTS HERE
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
// draw bitmap
dstrect.x = 10;
dstrect.y = 20;
for(int i = 0; i < 5; i++){
dstrect.x = 0;
for(int j = 0; j < 5; j++){
dstrect.x += r->w;
int k = grid[i][j];
SDL_Surface * pic;
// set the color
if(k == 0) pic = r;
else if(k == 1) pic = b;
else if(k == 2) pic = w;
else if(k == 3) pic = y;
else pic = g;
SDL_BlitSurface(pic, 0, screen, &dstrect);
}
dstrect.y +=r->h;
}
// update screen
SDL_UpdateWindowSurface( gWindow );
//test if player has won, if win the exit the game loop.
done |= didWin(jdll, jInstance);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(r);
SDL_FreeSurface(b);
SDL_FreeSurface(g);
SDL_FreeSurface(w);
SDL_FreeSurface(y);
for(int i = 0; i < 5; i++){
free(grid[i]);
}
free(grid);
for(int i = 0; i < 4;i++){
free(pt[i]);
}
free(pt);
free(jStruct);
//free J instance
jFree(jdll, jInstance);
// exit
printf("Exited cleanly\n");
return 0;
}