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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <SDL/SDL_mixer.h>
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/lexical_cast.hpp>
//#include "class1.h"
//g++ main.cpp -lSDL -lSDL_ttf -lSDL_mixer
//Sprite Color Border Object Kollision Location
//using namespace std;
//FUNCTIONS
int rectCollision(SDL_Rect* rect1,SDL_Rect* rect2){
if (rect1->x+rect1->w < rect2->x) return 0;
else if(rect1->x > rect2->x+rect2->w)return 0;
else if(rect1->y+rect1->h < rect2->y) return 0;
else if(rect1->y > rect2->y+rect2->h)return 0;
int colL=abs(rect1->x+rect1->w - rect2->x);
int colR=abs(rect1->x - rect2->x+rect2->w);
int colU=abs(rect1->y+rect1->h - rect2->y);
int colD=abs(rect1->y - rect2->y+rect2->h);
if(colL<=colR&&colL<=colU&&colL<=colD)return 1;
else if (colR<=colU&&colR<=colD)return 2;
else if (colU<=colD)return 3;
else return 4;
}
int getIndex(int col, int row, int ncol){ return row*ncol+col;}
void setBorder(SDL_Rect* clip,int x1,int y1,int x2,int y2){
int w=x2/x1;
int h=y2/y1;
for(int i=0; i<x1; i++){
for(int j=0; j<y1; j++){
clip[getIndex(i,j,x1)].x=w*i;
clip[getIndex(i,j,x1)].y=h*j;
clip[getIndex(i,j,x1)].w=w;//w*(i+1)-1;
clip[getIndex(i,j,x1)].h=h;//h*(i+1)-1;
//clip[i][j].x=w*i;
//clip[i][j].y=h*j;
//clip[i][j].w=w;//w*(i+1)-1;
//clip[i][j].h=h;//h*(i+1)-1;
}
}
}
//MAIN SCOPE
//int main(){
int main(int argc,char** argv){
//int main(int argc, char* args[]){
//SDL INIT
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Surface* screen,*text,*icon,*image,*obj2S;
screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
//SDL_DOUBLEBUF
//SDL_D...|FULLSCREEN
SDL_WM_SetIcon(icon,NULL);
SDL_WM_SetCaption("Hello World!",NULL);
//SDL_TTF INIT
TTF_Init();//Init ttf
TTF_Font* fontDef=TTF_OpenFont("setbackt.ttf",100);//make font
SDL_Color fontC={0,0,0};//font color
//int foo=5;
std::stringstream fontTxt;
//fontTxt<<"foo is " << foo;
//std::string fontTxt="foo is "+boost::lexical_cast<std::string>(foo);
//char fontTxt[10];
//sprintf(fontTxt,"%d",foo);
text=TTF_RenderText_Solid(fontDef,"hi",fontC);//set font
SDL_Rect fontL;//fonts location
fontL.x=170;
fontL.y=170;
//SDL_MIXER INIT
Mix_Chunk* effect1;
Mix_Music* music1;
Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
music1=Mix_LoadMUS("music1.wav");
effect1=Mix_LoadWAV("effect1.wav");
//FPS
Uint32 start;
Uint32 start2;
const int FPS=30;
int FPS2;
int FPS3;
int FPScounter=FPS;
int FPSsel=1;
//KEY VARIABLES
bool keyMouseL=0;
bool keyMouseR=0;
bool keyLeft=0;
bool keyRight=0;
bool keyUp=0;
bool keyDown=0;
//MISC VARIABLES
SDL_Rect mouseXY;
mouseXY.w=70;
mouseXY.h=17;
Uint32 rectC=SDL_MapRGB(screen->format,0xff,0x00,0x00);
Uint32 bgC=SDL_MapRGB(screen->format,0xaa,0xaa,0xff);
//OBJ2 DATA
SDL_Rect obj2XY;//Location
obj2XY.x=170;
obj2XY.y=170;
int obj2AX=11;//Sprite X and Y
int obj2AY=1;
SDL_Rect obj2B[obj2AX*obj2AY];//[obj2AX][obj2AY];//Border
setBorder(obj2B,obj2AX,obj2AY,932,68);
int obj2Spd=10;//Movement Speed
int obj2FX=-1;//Frame
int obj2FY=0;
obj2S=SDL_DisplayFormat(SDL_LoadBMP("low.bmp"));//image
SDL_SetColorKey(obj2S,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));//transparency
//LOADING IMAGE
image=SDL_DisplayFormat(SDL_LoadBMP("bmp.bmp"));
SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0x00));
//GAME LOOP
bool running = true;
while(running){
start=SDL_GetTicks();
//INPUT
//INPUT INIT
SDL_Event event;
while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running=false;
break;
//MOUSE MOTION
case SDL_MOUSEMOTION:
mouseXY.x= event.motion.x;
mouseXY.y= event.motion.y;
break;
//MOUSE DOWN
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button){
case SDL_BUTTON_LEFT:
keyMouseL=1;
break;
case SDL_BUTTON_RIGHT:
keyMouseR=1;
break;
}
mouseXY.x= event.motion.x;
mouseXY.y= event.motion.y;
break;
//MOUSE UP
case SDL_MOUSEBUTTONUP:
switch(event.button.button){
case SDL_BUTTON_LEFT:
keyMouseL=0;
break;
case SDL_BUTTON_RIGHT:
keyMouseR=0;
break;
}
mouseXY.x= event.motion.x;
mouseXY.y= event.motion.y;
break;
//KEYDOWN
case SDL_KEYDOWN:
switch(event.key.keysym.sym){
case SDLK_ESCAPE:
SDL_Quit();
case SDLK_LEFT:
keyLeft=1;
break;
case SDLK_RIGHT:
keyRight=1;
break;
case SDLK_UP:
keyUp=1;
break;
case SDLK_DOWN:
keyDown=1;
break;}
break;
//KEYUP
case SDL_KEYUP:
switch(event.key.keysym.sym){
case SDLK_LEFT:
keyLeft=0;
break;
case SDLK_RIGHT:
keyRight=0;
break;
case SDLK_UP:
keyUp=0;
break;
case SDLK_DOWN:
keyDown=0;
break;
case SDLK_1:
Mix_PlayMusic(music1,0);
break;
case SDLK_2:
Mix_PlayChannel(-1,effect1,0);//ch,effect,reps
break;
case SDLK_3:
break;
case SDLK_4:
break;}
}//s event.type
}//w polling
//LOGIC
//KEY LOGIC
if(keyLeft){ obj2XY.x-=obj2Spd;}
if(keyRight){ obj2XY.x+=obj2Spd;}
if(keyUp){ obj2XY.y-=obj2Spd;}
if(keyDown){ obj2XY.y+=obj2Spd;}
//FRAME LOGIC
/*if(obj2FX<obj2AX){
obj2FX++;
}else if(obj2FY<obj2AY){
obj2FY++;
obj2FX=0;
}else{
obj2FY=0;
obj2FX=0;
}*/
if(obj2FX<obj2AX*obj2AY-1)
obj2FX++;
else
obj2FX=0;
//OUTPUT
SDL_FillRect(screen,&screen->clip_rect,bgC);//BG color is bgC
SDL_FillRect(screen,&mouseXY,rectC);//rectangle/mouse
//image,border,screen,location
SDL_BlitSurface(image,NULL,screen,NULL);//image
SDL_BlitSurface(obj2S,&obj2B[obj2FX],screen,&obj2XY);//obj2
SDL_BlitSurface(text,NULL,screen,&fontL);//text
SDL_Flip(screen);
//FPS STUFF
if(1000/FPS>SDL_GetTicks()-start)
SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
FPS2 = (1000/(SDL_GetTicks()-start));
if(FPScounter>=FPS-1){
FPS3=SDL_GetTicks()-start2;
start2=SDL_GetTicks();
FPScounter=0;
if(FPSsel==1){FPSsel=0;}else{}
}else{
FPScounter+=1;}
//running=false;
}//w running
//QUIT PROGRAM
SDL_FreeSurface(image);
SDL_FreeSurface(text);
Mix_FreeChunk(effect1);
Mix_FreeMusic(music1);
Mix_CloseAudio();
TTF_CloseFont(fontDef);
TTF_Quit();
SDL_Quit();
return 0;
}
|