bool Enemy::CheckCollision(Vector3 pos) { SDL_Rect* result = new SDL_Rect(); Vector3 objPos = _transform.position; SDL_Rect objRect = SDL_Rect(); objRect.x = objPos.x; objRect.y = objPos.y; objRect.w = 1; objRect.h = 1; SDL_Rect targetRect = SDL_Rect(); targetRect.x = pos.x; targetRect.y = pos.y; targetRect.w = 1; targetRect.h = 1; bool enemyCollision = SDL_IntersectRect(&objRect, &targetRect, result); if (enemyCollision) return true; else { objPos = _projectile->GetTransform().position; objRect.x = objPos.x; objRect.y = objPos.y; objRect.w = 1; objRect.h = 1; return SDL_IntersectRect(&objRect, &targetRect, result); } }
void Surface::blit(Surface *m_surface, Rectangle *src, Rectangle *dst) { SDL_Rect sdl_src = SDL_Rect(); SDL_Rect sdl_dst = SDL_Rect(); Rectangle::Copy(src, &sdl_src); Rectangle::Copy(dst, &sdl_dst); blit(m_surface, &sdl_src, &sdl_dst); }
void Surface::blit(Surface *m_surface, Rectangle *dst) { SDL_Rect sdl_dst = SDL_Rect(); Rectangle::Copy(dst, &sdl_dst); SDL_Rect src = SDL_Rect(); src.w = m_surface->getWidth(); src.h = m_surface->getHeight(); blit(m_surface, &src, &sdl_dst); }
void RadioButton::Init(Gui &gui) { setMode(normal); mPositionTex.push_back(SDL_Rect(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["UncheckedImage"]->GetSDLPart())); mPositionTex.push_back(SDL_Rect(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["CheckedImage"]->GetSDLPart())); mPositionTex.push_back(SDL_Rect(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["UncheckedHoverImage"]->GetSDLPart())); mPositionTex.push_back(SDL_Rect(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["CheckedHoverImage"]->GetSDLPart())); mColor.push_back(Vector3i(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["TextColorNormal"]->GetColor())); mColor.push_back(Vector3i(ResourceManager::GetManagerResource()->GetResource()["RadioButton"]["TextColorHover"]->GetColor())); SDL_Rect temp = mPositionTex[0]; setSize(temp.w,temp.h); }
Sprite::Sprite() : tickTime(0.f), currentFrame(0), noRender(false), loop(true), animMode(ANIMATE_FRAMES), paused(false), stretch(false), velx(0), vely(0), pos(Vector2D()), imgRect(SDL_Rect()), startingRect(SDL_Rect()) { tickTime = SDL_GetTicks(); globalTime = tickTime; }
Window::Window(const int width, const int height, const int cellSize) { if (SDL_Init(SDL_INIT_EVERYTHING) == -1) { printError(); } // Board needs to be setup before the window because I need the size of the board setupBoard(width, height, cellSize); window = SDL_CreateWindow("Game of life", 0, 0, boardBackground.w + 14, boardBackground.h + 57, SDL_WINDOW_SHOWN); if (window == NULL) { printError(); } renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if (renderer == NULL) { printError(); } // Load UI image textures[UI] = IMG_LoadTexture(renderer, "../../../../../../../../dev/CPP/Life/StaticUI.png"); dstRects[UI] = {(boardBackground.w / 2) - (463 / 2), boardBackground.y + boardBackground.h + 10, 463, 26}; // Setup UI clip rects for (int i = 0; i < 4; i++) { UIClipRects.push_back(SDL_Rect()); UIClipRects[i].x = dstRects[UI].x + i * 118; UIClipRects[i].y = dstRects[UI].y; UIClipRects[i].w = 110; UIClipRects[i].h = 26; } }
TGfxSprite::TGfxSprite() { m_pTexture = nullptr; m_tCut = SDL_Rect(); m_iPosX = 0; m_iPosY = 0; }
void Surface::blit(Surface *m_surface, SDL_Rect *dst) { SDL_Rect src = SDL_Rect(); src.w = m_surface->getWidth(); src.h = m_surface->getHeight(); blit(m_surface, &src, dst); }
void Surface::fill(Uint32 c) { SDL_Rect region = SDL_Rect(); region.x = 0; region.y = 0; region.w = width; region.h = height; int rs = SDL_FillRect(sdlSurface, ®ion, c); }
Surface* ActionBind::render(void) { Surface *buffer = new Surface(width, height); if(focus) { buffer->fill(highlightBackground); } else { buffer->fill(background); } SDL_Rect src = SDL_Rect(); SDL_Rect dst = SDL_Rect(); stringBuffer->updateSDLRect(&src); stringBuffer->updateSDLRect(&dst); dst.x = (width - dst.w) / 2; dst.y = (height - dst.h) / 2; buffer->blit(stringBuffer, &src, &dst); PRect border = PRect(); border.setX(0); border.setY(0); border.setWidth(width - 1); border.setHeight(height - 1); Color *c_border = foreground; if(focus) { c_border = highlightForeground; } border.draw(buffer->getSDLSurface(), c_border); return buffer; }
void Player::Render(SDL_Renderer* gRenderer, b2Vec2 offset) { rect.x = dynamicBody->GetPosition().x -(rect.w/2.0f)- offset.x; rect.y = dynamicBody->GetPosition().y -(rect.h/2.0f)+ offset.y; SDL_Rect h = SDL_Rect(); h.x =0; h.y =0; h.w = 165; h.h = 230; SDL_RenderCopy( gRenderer, m_texture, &h, &rect ); }
//----------------- bool CBarrier::init( const TPoint2 & pos, const TPoint2 & size ) { m_texture.loadFromFile( "data/textures/blank.png" ); m_texture.setPosition( pos ); m_texture.setClip( SDL_Rect( 0, 0, size.x, size.y ) ); colors[ 2 ] = SDL_Color( 0, 255, 0, 255 ); colors[ 1 ] = SDL_Color( 255, 255, 0, 255 ); colors[ 0 ] = SDL_Color( 255, 0, 0, 255 ); reset( ); return true; }
Sprite::Sprite(std::string filename, std::string name, GLfloat x, GLfloat y) : GameComponent(name), texture(0) { this->filename = filename; positionX = x; positionY = y; scaleX = 1; scaleY = 1; rotation = 0; bounds = SDL_Rect(); drawColor[0] = drawColor[1] = drawColor[2] = drawColor[3] = 1.0; }
CrabEnemy::CrabEnemy() : Enemy() { velocity = 64.0f; sprite = new Sprite(GraphicsEngine::GetInstance()); sprite->SetTexture("player.png"); SDL_Rect r = SDL_Rect(); r.x = 0; r.y = 0; r.w = 64; r.h = 64; sprite->SetBounds(r); r.x = (int)GetPosition().first; r.y = (int)GetPosition().second; sprite->SetPosition(r); Move(64 * 4, 64 * 5); }
//Draws the specified texture //param:texture->the index given from load texture //param:color->the tint to apply to the texture //param:sourceRect->the rectangle to pull a smaller image from the texture with. NULL for the whole texture //param:position->the point to draw to //param:angle->the rotation to apply //param:scale->the scale to apply. This is uniform across x and y //param:flip->SDL_RendererFlip::None/FlipHorizontal/FlipVertical. If you want both Flip Horizontal and Flip Vertical, //use the bitwise operator '|' //param:origin->Origin to rotate around. If NULL, uses center of destRect created by position and scale //param:layerDepth->The depth to draw the image at //returns -1 on error, 0 for success int SpriteBatch::DrawTexture(Uint32 texture, SDL_Color color, const SDL_Rect* sourceRect, const SDL_Point* position, float angle, float scale, SDL_RendererFlip flip, const SDL_Point* origin, float layerDepth) { //Check if spritebatch.begin has been called if(!begun) { std::cout<<"Begin must be called before attempting to draw"<<std::endl; return -1; } ///create a temporary point SDL_Point pos = SDL_Point(); //if position is null, set our temp to 0,0 if(!position) { pos = CreatePoint(0, 0); } else { //otherwise set with position pos = CreatePoint(position->x, position->y); } SDL_Rect destRect = SDL_Rect(); //If we were given a source rectangle if(sourceRect) { //create a dest rectangle using position and source rectangle's dimensions destRect = CreateRect(pos.x, pos.y, (int)(sourceRect->w * scale), (int)(sourceRect->h * scale)); } else { int w = 0; int h = 0; //get the width and height SDL_QueryTexture(textureList[texture], NULL, NULL, &w, &h); //create a dest rect using position and the image dimensions destRect = CreateRect(pos.x, pos.y, (int)(w * scale), (int)(h * scale)); } //Call other DrawTexture to do the actual drawing return DrawTexture(texture, color, sourceRect, &destRect, angle, origin, flip, layerDepth); }
void Window::setupBoard(const int width, const int height, const int cellSize) { // Black background rectangle boardBackground.x = 7; boardBackground.y = 7; boardBackground.w = width * cellSize + width * 3 + 3; boardBackground.h = height * cellSize + height * 3 + 3; // Blue rectangles to show alive cells for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { boardCells.push_back(SDL_Rect()); boardCells[j + i * width].x = (j * cellSize + j * 3) + 10; boardCells[j + i * width].y = (i * cellSize + i * 3) + 10; boardCells[j + i * width].w = cellSize; boardCells[j + i * width].h = cellSize; } } }
Player::Player(b2World* world, SDL_Renderer* gRenderer, b2Vec2 position, b2Vec2 dimentions) : isSpaceDown(false) { myBodyDef.type = b2_dynamicBody; myBodyDef.position.Set(position.x , position.y ); myBodyDef.userData = (void*)0; myBodyDef.angularDamping = 2; dynamicBody = world->CreateBody(&myBodyDef); playerShape.SetAsBox(dimentions.x, dimentions.y); fixtureDef.shape = &playerShape; fixtureDef.filter.groupIndex = -1; fixtureDef.density = 0.1; dynamicBody->CreateFixture(&fixtureDef); m_KeyboardMan = KeyboardManager::getKeys(); m_texture = IMG_LoadTexture(gRenderer,"images/player.png"); rect = SDL_Rect(); rect.h = dimentions.x*2; rect.w = dimentions.y*2; sm = new SoundManager(); sm->PlayBackground(); count = 0; }
SDL_Rect Sprite::GetRect() const { SDL_Rect ret = SDL_Rect(); if(animMode == ANIMATE_SHEET) { ret.h = startingRect.h; ret.w = startingRect.w; ret.x = pos.x; ret.y = pos.y; return ret; } else if(!m_frames.empty()) { ret.h = m_frames[currentFrame].Texture->h; ret.w = m_frames[currentFrame].Texture->w; ret.x = pos.x; ret.y = pos.y; return ret; } else return ret; }
SDL_Rect Font::textBounds(std::string text) { if (text.length() == 0) { return SDL_Rect(); } Color c; SDL_Surface *surf = TTF_RenderText_Blended(font_, text.c_str(), c.getSDLColor()); SDL_Rect r; if (surf == 0) { logSDLError(std::cout, "TTF_RenderText"); r.x = 0; r.y = 0; r.w = 0; r.h = 0; return r; } r = surf->clip_rect; SDL_FreeSurface(surf); return r; }
/* Create Camera at ( x, y ) */ Camera::Camera( double x, double y ) : _x( 0.0 ), _y( 0.0 ), _w( 0.0 ), _h( 0.0 ), _left( 0.0 ), _right( 0.0 ), _top( 0.0 ), _bottom( 0.0 ), _rect( SDL_Rect() ), _cache_half_screen_w( (double)( SCREEN_W/SCALE )/2.0 ), _cache_half_screen_h( (double)( SCREEN_H/SCALE )/2.0 ) { _w = _cache_half_screen_w; _h = _cache_half_screen_h; /* Move Camera to ( x, y ) */ SetXY( x, y ); };
int main( int argc, char* args[] ) { SDL_Color BACKGROUND_COLOR = SDL_Color(); BACKGROUND_COLOR.r = 0; BACKGROUND_COLOR.g = 0; BACKGROUND_COLOR.b = 0; BACKGROUND_COLOR.a = 255; SDL_Color HUNTER_COLOR = SDL_Color(); HUNTER_COLOR.r = 255; HUNTER_COLOR.g = 0; HUNTER_COLOR.b = 0; HUNTER_COLOR.a = 255; SDL_Color BOUND_COLOR = SDL_Color(); BOUND_COLOR.r = 0; BOUND_COLOR.g = 255; BOUND_COLOR.b = 0; BOUND_COLOR.a = 255; SDL_Color PREY_COLOR = SDL_Color(); PREY_COLOR.r = 0; PREY_COLOR.g = 0; PREY_COLOR.b = 255; PREY_COLOR.a = 255; // Init GUI SDL_Window *screen = NULL; if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { std::cout << "Error on SDL_Init:" << std::endl << SDL_GetError() << std::endl; return 1; } screen = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0); if( screen == NULL ) { std::cout << "Error on SDL_CreateWindow:" << std::endl << SDL_GetError() << std::endl; return 1; } SDL_Renderer* bgRenderer = NULL; bgRenderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if( bgRenderer == NULL ) { std::cout << "Error on SDL_CreateRenderer:" << std::endl << SDL_GetError() << std::endl; return 1; } // Define neural network LinearNeuralNetwork nn = LinearNeuralNetwork( 0, { 1 }, NULL, NULL ); if(std::ifstream(FILENAME)) { nn = LinearNeuralNetwork( FILENAME, Neuron::SIGMOID, Neuron::SIGMOID_DER ); } else { nn = LinearNeuralNetwork( ALPHA, NEURAL_NETWORK_DESCRIPTOR, Neuron::SIGMOID, Neuron::SIGMOID_DER ); } // Inizialization SDL_Rect bounds = SDL_Rect(); bounds.x = BOUND_MIN_X; bounds.y = BOUND_MIN_Y; bounds.w = BOUND_MAX_X - BOUND_MIN_X; bounds.h = BOUND_MAX_Y - BOUND_MIN_Y; Entity hunter = Entity( HUNTER_WIDTH, HUNTER_HEIGHT, HUNTER_SPEED, HUNTER_START_X, HUNTER_START_Y, bounds, HUNTER_COLOR ); Entity prey = Entity( PREY_WIDTH, PREY_HEIGHT, PREY_SPEED, PREY_START_X, PREY_START_Y, bounds, PREY_COLOR ); // Render clr(bgRenderer,BACKGROUND_COLOR); renderRect(bounds, BOUND_COLOR, bgRenderer, false); renderEntity(hunter, bgRenderer); renderEntity(prey, bgRenderer); SDL_RenderPresent(bgRenderer); // TIMERS SDL_TimerID renderTimerId = SDL_AddTimer(TIME_GAP_RENDER, render_callback, NULL); if( renderTimerId == 0 ) { std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl; return 1; } SDL_TimerID movementTimerId = SDL_AddTimer(TIME_GAP_MOVEMENT, movement_callback, NULL); if( movementTimerId == 0 ) { std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl; return 1; } // main cycle //// structure to read user events SDL_Event event; //// to check when the key is pressed or released bool isKeyPressed[4] = { false, false, false, false }; bool quit = false; while( quit == false ) { bool matchIsOver = false; while(!matchIsOver) { // event handling while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: // Keyboard input handling - keydown checkKeyPressed( event.key.keysym.sym, isKeyPressed, true ); break; case SDL_KEYUP: // Keyboard input handling - keyup checkKeyPressed( event.key.keysym.sym, isKeyPressed, false ); break; case SDL_USEREVENT: switch(event.user.code) { case RENDER_CB: // Render clr(bgRenderer,BACKGROUND_COLOR); renderRect(bounds, BOUND_COLOR, bgRenderer, false); renderEntity(hunter, bgRenderer); renderEntity(prey, bgRenderer); SDL_RenderPresent(bgRenderer); break; case MOVEMENT_CB: int initialDistance = dist(hunter,prey); // Entities movement hunter.move( actHunter(hunter,prey) ); //userMovement(prey, isKeyPressed); // DEBUG std::vector<float> outputNn = outPrey( hunter, prey, nn ); Entity::Action preyAction = actPrey( outputNn ); prey.move( preyAction ); // check contact if( checkContact( hunter, prey ) ) { std::vector<float> err (5, 0); err[preyAction] = - REWARD_CAPTURE * outputNn[preyAction]; nn.learn( err ); matchIsOver = true; } else { int distance = dist(hunter, prey); // std::cout << "distances" << distance << " " << initialDistance << std::endl; std::vector<float> err (5, 0); if( distance > initialDistance ) { err[preyAction] = ( 1 - outputNn[preyAction] ) * REWARD_DISTANCE; } else if( distance < initialDistance ) { err[preyAction] = - outputNn[preyAction] * REWARD_DISTANCE; } nn.learn( err ); } break; // default: // std::cout << "!!! UserEvent code not defined!!!" << std::endl; // return 1; //break; } break; default: break; } } } hunter.reset(); prey.reset(); } SDL_Quit(); return 0; }
int main( int argc, char* args[] ) { SDL_Color BACKGROUND_COLOR = SDL_Color(); BACKGROUND_COLOR.r = 0; BACKGROUND_COLOR.g = 0; BACKGROUND_COLOR.b = 0; BACKGROUND_COLOR.a = 255; SDL_Color HUNTER_COLOR = SDL_Color(); HUNTER_COLOR.r = 255; HUNTER_COLOR.g = 0; HUNTER_COLOR.b = 0; HUNTER_COLOR.a = 255; SDL_Color BOUND_COLOR = SDL_Color(); BOUND_COLOR.r = 0; BOUND_COLOR.g = 255; BOUND_COLOR.b = 0; BOUND_COLOR.a = 255; SDL_Color PREY_COLOR = SDL_Color(); PREY_COLOR.r = 0; PREY_COLOR.g = 0; PREY_COLOR.b = 255; PREY_COLOR.a = 255; // Init GUI SDL_Window *screen = NULL; if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { std::cout << "Error on SDL_Init:" << std::endl << SDL_GetError() << std::endl; return 1; } screen = SDL_CreateWindow(WINDOW_NAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, 0); if( screen == NULL ) { std::cout << "Error on SDL_CreateWindow:" << std::endl << SDL_GetError() << std::endl; return 1; } SDL_Renderer* bgRenderer = NULL; bgRenderer = SDL_CreateRenderer( screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); if( bgRenderer == NULL ) { std::cout << "Error on SDL_CreateRenderer:" << std::endl << SDL_GetError() << std::endl; return 1; } // Define QLAgent QLAgent<QLAction,QLState>::qlt table; if(std::ifstream(FILENAME)) { loadQLTable(table,FILENAME); } const std::vector<QLAction> actionVec { Entity::UP, Entity::DOWN, Entity::LEFT, Entity::RIGHT, Entity::IDLE }; QLAgent<QLAction,QLState> preyAgent = QLAgent<QLAction,QLState>([&actionVec](QLState u){return actionVec;} , table); preyAgent.setQLParameters(CAP, LR, DF, GVR); // Inizialization SDL_Rect bounds = SDL_Rect(); bounds.x = BOUND_MIN_X; bounds.y = BOUND_MIN_Y; bounds.w = BOUND_MAX_X - BOUND_MIN_X; bounds.h = BOUND_MAX_Y - BOUND_MIN_Y; Entity hunter = Entity( HUNTER_WIDTH, HUNTER_HEIGHT, HUNTER_SPEED, HUNTER_START_X, HUNTER_START_Y, bounds, HUNTER_COLOR ); Entity prey = Entity( PREY_WIDTH, PREY_HEIGHT, PREY_SPEED, PREY_START_X, PREY_START_Y, bounds, PREY_COLOR ); preyAgent.setCurrentState(dataToQLState(hunter,prey)); // Render clr(bgRenderer,BACKGROUND_COLOR); renderRect(bounds, BOUND_COLOR, bgRenderer, false); renderEntity(hunter, bgRenderer); renderEntity(prey, bgRenderer); SDL_RenderPresent(bgRenderer); // TIMERS SDL_TimerID renderTimerId = SDL_AddTimer(TIME_GAP_RENDER, render_callback, NULL); if( renderTimerId == 0 ) { std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl; return 1; } SDL_TimerID movementTimerId = SDL_AddTimer(TIME_GAP_MOVEMENT, movement_callback, NULL); if( movementTimerId == 0 ) { std::cout << "Error on SDL_AddTimer:" << std::endl << SDL_GetError() << std::endl; return 1; } // main cycle //// structure to read user events SDL_Event event; //// to check when the key is pressed or released bool isKeyPressed[4] = { false, false, false, false }; bool quit = false; int matchCounter = 0; while( quit == false ) { bool matchIsOver = false; matchCounter++; // int moveCounter = 0; // int succesCounter = 0; while(!matchIsOver) { // event handling while( SDL_PollEvent( &event ) ) { switch( event.type ) { case SDL_QUIT: quit = true; break; case SDL_KEYDOWN: // Keyboard input handling - keydown checkKeyPressed( event.key.keysym.sym, isKeyPressed, true ); break; case SDL_KEYUP: // Keyboard input handling - keyup checkKeyPressed( event.key.keysym.sym, isKeyPressed, false ); break; case SDL_USEREVENT: switch(event.user.code) { case RENDER_CB: // Render clr(bgRenderer,BACKGROUND_COLOR); renderRect(bounds, BOUND_COLOR, bgRenderer, false); renderEntity(hunter, bgRenderer); renderEntity(prey, bgRenderer); SDL_RenderPresent(bgRenderer); break; case MOVEMENT_CB: // Entities movement hunter.move( actHunter(hunter,prey) ); //userMovement(prey, isKeyPressed); prey.move( preyAgent.chooseAction() ); // check contact if( checkContact( hunter, prey ) ) { hunter.reset(); prey.reset(); preyAgent.update(dataToQLState(hunter,prey), CATCH_REWARD); matchIsOver = true; } else { preyAgent.update(dataToQLState(hunter,prey), SURVIVE_REWARD); } break; } break; default: break; } } // // moveCounter++; // // hunter.move( actHunter(hunter,prey) ); // //userMovement(prey, isKeyPressed); // // prey.move( preyAgent.chooseAction() ); // // // check contact // if( checkContact( hunter, prey ) ) { // hunter.reset(); // prey.reset(); // preyAgent.update(dataToQLState(hunter,prey), CATCH_REWARD); // matchIsOver = true; // } // else { // preyAgent.update(dataToQLState(hunter,prey), SURVIVE_REWARD); // } // // if(matchCounter%10000 == 0) { std::cout << matchCounter << std::endl; } // if(matchCounter%100000 == 0) { quit = true; } // if(moveCounter == 1000) { succesCounter++; matchIsOver = true; } // if(succesCounter == 100) { std::cout << "SUCCESS!!!" << std::endl; } } } std::cout << "states in table: " << table.size() << std::endl; saveQLTable(table,FILENAME); SDL_Quit(); return 0; }