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
|
#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;
class Vector2{
public:
int x;
int y;
Vector2(int x,int y): x(x),y(y){}
Vector2(){x = 0;y = 0;}
void add(Vector2& other){
x += other.x;
y += other.y;
}
void scale(double scalar){
x*=scalar;
y*=scalar;
}
};
Vector2 addVectors(const Vector2& one,const Vector2& two){
Vector2 difference;
difference.x = one.x + two.x;
difference.y = one.y + two.y;
return difference;
}
class BlackRect{
public:
bool objectLeft = false;
bool objectRight = true;
Vector2 vec;
SDL_Rect spriteOneRect;
SDL_Surface* spriteOneSurface;
SDL_Texture* spriteOneTexture;
BlackRect(int x,int y,string fileName){
vec.x = x;
vec.y = y;
spriteOneSurface = IMG_Load(fileName.c_str());
spriteOneTexture = SDL_CreateTextureFromSurface(renderer,spriteOneSurface);
spriteOneRect.x = x;
spriteOneRect.y = y;
SDL_QueryTexture(spriteOneTexture,NULL,NULL,&spriteOneRect.w,&spriteOneRect.h);
}
BlackRect(){
vec.x = 250;
vec.y = 250;
}
void update(){
spriteOneRect.x = vec.x;
spriteOneRect.y = vec.y;
}
};
void render(BlackRect& br,BlackRect& rr){
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderCopy(renderer,br.spriteOneTexture,NULL,&br.spriteOneRect);
SDL_RenderCopy(renderer,rr.spriteOneTexture,NULL,&rr.spriteOneRect);
SDL_RenderPresent(renderer);
}
int SDL_main(int argc,char* argv[]){
init(); // init not included in snippet
bool quit = false;
bool jumping = false;
BlackRect br(20,400,"blockone.png");
BlackRect rr(400,200,"blockoneRed.png");
Vector2 gravity(0,1);
Vector2 velocity(2,-20);
Vector2 minusTwoPos(-20,-400);
Vector2 difference = addVectors(rr.vec,minusTwoPos);
cout << difference.x << " y == " << difference.y << endl;
while(true)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
cleanUp();
return 0;
}
if(event.type == SDL_KEYDOWN)
{
int key = event.key.keysym.sym;
if(key == SDLK_b)
moveObject(br);
else if(key == SDLK_c){
br.vec.add(difference);
br.update();
}
}
}
if(jumping)
{
br.vec.add(velocity);
br.update();
velocity.add(gravity);
jumpSecond(br,400,jumping,velocity);
}
render(br,rr);
SDL_Delay(20);
}
cleanUp(); // clean up not included in snippet
}
| |