Question: I m creating a helicopter game using SDL 2 and C + + language in Visual Studio 2 0 2 2 . The code created

Im creating a helicopter game using SDL2and C++language in Visual Studio2022. The code created does load the program but it closes immediately, I assume it's SDL_QUIT. I want it to load with both images (player1.png, background.png), but it doesn't do that. Most importantly, I need help in a sample code that moves the helicopter (player1.png) foward as the code runs, to get a better idea on how the program would display/work. Here is my code. //main.cpp
#include "GameLoop.h"
GameLoop g;
int main(int argc, char** argv){
g.Initialize();
while (g.getGameState()){
g.Render();
g.Update();
g.Event(); // Call event handling function in each iteration
}
g.Clear();
return 0;
}
// gameloop.cpp
#include "GameLoop.h"
GameLoop::GameLoop(){
window = NULL;
renderer = NULL;
//srcPlayer = NULL;
//destPlayer = NULL;
Player = NULL;
background = NULL;
GameState = false;
}
bool GameLoop::getGameState()
{
return GameState;
}
void GameLoop::Initialize(){
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Helicopter Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
if (window){
renderer = SDL_CreateRenderer(window,-1,0);
if (renderer){
cout "Succeeded!" endl;
Player = TextureManager::Texture("image/player1.png", renderer);
background = TextureManager::Texture("image/background.png", renderer);
}
else {
cout "Renderer not created!" endl;
}
}
else {
cout "Window not created!" endl;
}
}
void GameLoop::Update(){
//source dimension
srcPlayer.h =30;
srcPlayer.w =90;
srcPlayer.x = srcPlayer.y =0;
//destination dimension
destPlayer.h =90;
destPlayer.w =300;
destPlayer.x = destPlayer.y =20;
// Update helicopter position based on input events
// For simplicity, let's assume the helicopter moves 1 unit forward and downward in each frame
//destPlayer.x +=100; // Move forward
//destPlayer.y +=100; // Move downward
}
// Add event handling function to handle user input
void GameLoop::Event(){
SDL_Event event;
while (SDL_PollEvent(&event)){
switch (event.type){
case SDL_QUIT:
GameState = false;
break;
// Add cases to handle other events, such as key presses or mouse movements
}
}
}
void GameLoop::Render(){
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, Player, &srcPlayer, &destPlayer);
SDL_RenderPresent(renderer);
}
void GameLoop::Clear(){
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
//GameLoop.h
#pragma once
#include
#include
#include
#include "TextureManager.h"
using namespace std;
class GameLoop
{
private:
const int HEIGHT =640;
const int WIDTH =800;
bool GameState;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* Player;
SDL_Texture* background;
SDL_Rect srcPlayer{}, destPlayer{};
public:
GameLoop();
bool getGameState();
void Update();
void Initialize();
void Event();
void Render();
void Clear();
};
//TextureManages.cpp
#include "TextureManager.h"
SDL_Texture* TextureManager::Texture(const char* filelocation, SDL_Renderer* ren)
{
SDL_Surface* surface;
surface = IMG_Load(filelocation);
SDL_Texture* tex;
tex = SDL_CreateTextureFromSurface(ren, surface);
return tex;
}
//TextureManager.h
#pragma once
#include
#include
#include"GameLoop.h"
class TextureManager
{
public:
static SDL_Texture* Texture(const char* filelocation, SDL_Renderer* ren);
};
I m creating a helicopter game using SDL 2 and C

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!