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
|
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
using namespace std;
SDL_Renderer* renderer;
SDL_Window* window;
SDL_Texture* texture;
SDL_Surface* surface;
SDL_Event event;
SDL_Surface* spriteOneSurface;
SDL_Surface* spriteTwoSurface;
SDL_Texture* spriteOneTexture;
SDL_Texture* spriteTwoTexture;
SDL_Rect spriteOneRect;
SDL_Rect spriteTwoRect;
bool spriteOneUp = false;
bool spriteOneDown = false;
bool spriteOneLeft = false;
bool spriteOneRight = true;
bool spriteTwoUp = false;
bool spriteTwoDown = true;
bool spriteTwoleft = false;
bool spiteTwoRight = false;
int velocity = 2;
int velocityFirstObj = 2;
void render(){
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderCopy(renderer,spriteOneTexture,NULL,&spriteOneRect);
SDL_RenderCopy(renderer,spriteTwoTexture,NULL,&spriteTwoRect);
SDL_RenderPresent(renderer);
}
void init(){
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
cout << "fail" << endl;
if(!IMG_Init(IMG_INIT_PNG))
cout << "image fail" << endl;
window = SDL_CreateWindow("sdl",250,250,800,600,SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
spriteOneSurface = IMG_Load("blockOne.png");
spriteTwoSurface = IMG_Load("blockTwo.png");
spriteOneTexture = SDL_CreateTextureFromSurface(renderer,spriteOneSurface);
spriteTwoTexture = SDL_CreateTextureFromSurface(renderer,spriteTwoSurface);
spriteOneRect.x = 20;
spriteOneRect.y = 20;
SDL_QueryTexture(spriteOneTexture,NULL,NULL,&spriteOneRect.w,&spriteOneRect.h);
spriteTwoRect.x = 350;
spriteTwoRect.y = 20;
SDL_QueryTexture(spriteTwoTexture,NULL,NULL,&spriteTwoRect.w,&spriteTwoRect.h);
}
void cleanUp(){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = NULL;
renderer = NULL;
SDL_Quit();
}
bool objectsColliding(SDL_Rect& one, SDL_Rect& two){
if(one.y >= two.y + two.h)
return false;
if(one.x >= two.x + two.w)
return false;
if(one.y + one.h <= two.y)
return false;
if(one.x + one.w <= two.x)
return false;
cout << "returning true :: collision" << endl;
return true;
}
bool collisionBounds(SDL_Rect &rect){
if(rect.y <= 0)
return true;
if(rect.y + rect.h >= 600)
return true;
if(rect.x <= 0)
return true;
if(rect.x + rect.w >= 800)
return true;
return false;
}
void moveObject(SDL_Rect& rect){
if(&rect == &spriteOneRect){
if(collisionBounds(rect)){
if(spriteOneLeft){
spriteOneLeft = false;
spriteOneRight = true;
}else{
spriteOneRight = false;
spriteOneLeft = true;
}
}
if(objectsColliding(spriteOneRect,spriteTwoRect)){
if(spriteOneLeft){
spriteOneLeft = false;
spriteOneRight = true;
}else{
spriteOneRight = false;
spriteOneLeft = true;
}
}
if(spriteOneLeft)
spriteOneRect.x-= velocityFirstObj;
else
spriteOneRect.x+= velocityFirstObj;
}
if(&rect == &spriteTwoRect){
if(collisionBounds(rect)){
if(spriteTwoDown){
spriteTwoDown = false;
spriteTwoUp = true;
}else{
spriteTwoUp = false;
spriteTwoDown = true;
}
}
if(objectsColliding(spriteOneRect,spriteTwoRect)){
if(spriteTwoUp){
spriteTwoUp = false;
spriteTwoDown = true;
}else{
spriteTwoDown = false;
spriteTwoUp = true;
}
}
if(spriteTwoDown)
spriteTwoRect.y+=2;
else
spriteTwoRect.y-=2;
}
}
int SDL_main(int argc,char* argv[]){
init();
bool quit = false;
while(!quit){
SDL_PollEvent(&event);
if(event.type == SDL_QUIT)
break;
if(event.type == SDL_KEYDOWN){
int bKey = event.key.keysym.sym;
if(bKey == SDLK_b)
velocityFirstObj += 2;
}
moveObject(spriteTwoRect);
moveObject(spriteOneRect);
SDL_Delay(2);
render();
}
cleanUp();
}
| |