0% found this document useful (0 votes)
52 views9 pages

Blue-Red Wars

The document describes a C++ program that implements a basic 2D game involving moving a red bazooka sprite and displaying text using SDL and other libraries. The program initializes SDL and loads resources like textures and fonts. It contains classes for the window, a rect object representing the bazooka sprite, and text drawing. Key presses move the bazooka and play sound effects, while the display is updated in the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views9 pages

Blue-Red Wars

The document describes a C++ program that implements a basic 2D game involving moving a red bazooka sprite and displaying text using SDL and other libraries. The program initializes SDL and loads resources like textures and fonts. It contains classes for the window, a rect object representing the bazooka sprite, and text drawing. Key presses move the bazooka and play sound effects, while the display is updated in the main loop.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BLUE-RED

WARS
resources

textura _fondo

rect
rect

_bazooka rect
resources.rc
#include <iostream>
#include<SDL.h>
#include"window.h"
#include"rect.h"
#include"text.h"
#include<Windows.h>
#include<Mmsystem.h>

#pragma comment(lib, "Winmm.lib")

void pollEvents(window &window, rect &rect)


{
SDL_Event event;
if (SDL_PollEvent(&event))
{
rect.pollEvents(event);
window.pollEvents(event);
}
}

int main(int argc, char **argv)


{
PlaySound(TEXT("E:\\ORDENADOR TORRE\\UNIVERSIDAD\\3º\\INFORMÁTICA
INDUSTRIAL\\TRABAJO\\TRABAJO\\TEXTURES_PROJECT\\TEXTURES_C++\\TEXTURES_C++\\res\\SOUND\\COMBAT\\COMBAT_THEME.wav"), NULL,
SND_FILENAME||SND_NOSTOP||SND_LOOP);
window window("BLUE/ RED war.", 800, 600);
rect rect(120, 120, 100, 100, "res/OBJECTS/RED/BAZOOKA_ESTATICO.png");
text text(window::_renderer, "res/Early GameBoy.ttf", 30, "\t\t\t\t\tADVANCED WARS",{0, 0, 0, 255});
while (!window.isClosed())
{
pollEvents(window,rect);
rect.draw();
text.display(20, 20, window::_renderer);
window.clear();
}
return 0;
}

#include "window.h"
#include<SDL_image.h>
#include<iostream>
#include<SDL_ttf.h>
SDL_Renderer*window::_renderer=nullptr;
window::window(const std::string &title, int _width, int _height):
_title(title),_width(_width), _height(_height)
{
_closed = !init();
}
window::~window()
{
SDL_DestroyRenderer(_renderer);
SDL_DestroyWindow(_window);
TTF_Quit();
IMG_Quit();
SDL_Quit();

bool window::init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cerr << "ERROR AL INICIAR SDL\n";
return false;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cerr<<"FALLO AL INICIAR LAS TEXTURAS DESDE IMAGEN.\n";
return false;
}
if (TTF_Init() == -1)
{
std::cerr << "FALLO AL INICIAR LAS TTF.\n";
return false;
}

_window = SDL_CreateWindow(
_title.c_str(),
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
_width, _height,
0
);
if (_window == nullptr)
{
std::cerr << "FALLO AL CREAR LA VENTANA.\n";
return false;
}

_renderer = SDL_CreateRenderer(_window, -1, SDL_RENDERER_ACCELERATED);


if (_renderer == nullptr)
{
std::cerr << "FALLO AL EJECUTAR EL RENDERIZADOR\n";
}
return true;
}

void window::pollEvents(SDL_Event &event)


{
switch (event.type)
{
case SDL_QUIT:
_closed = true;
break;

case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
{
_closed = true;
break;
}
default:
{
break;
}

}
}
}

void window::clear() const


{
SDL_RenderPresent(_renderer);
SDL_SetRenderDrawColor(_renderer, 200, 226, 42, 255);
SDL_RenderClear(_renderer);
}
#pragma once

#include<string>
#include<SDL.h>
class window {
public:
window(const std::string &title, int _width, int _height);
~window();
void pollEvents(SDL_Event &event);
void clear()const;
inline bool isClosed() const { return _closed; }
private:
bool init();

private:
std::string _title;
int _width=800;
int _height=600;

SDL_Window *_window = nullptr;


protected:

bool _closed = false;

public:
static SDL_Renderer*_renderer;
};

#pragma once

#include"window.h"

class rect{
public:
rect( int w, int h, int x, int y, int r, int g, int b, int a);
rect(int w, int h, int x, int y, const std::string &image_path);
~rect();
void draw()const;
void pollEvents(SDL_Event &event);
private:
int _w, _h;
int _x, _y;
int _r, _g, _b, _a;
SDL_Texture*_bazooka_texture = nullptr;

};
#include"rect.h"
#include<SDL_image.h>
#include<iostream>
#include<Windows.h>
#include<Mmsystem.h>
#pragma comment(lib, "Winmm.lib"

rect::rect( int w, int h, int x, int y, int r, int g, int b, int a) :


_w(w), _h(h), _x(x), _y(y), _r(r), _g(g), _b(b), _a(a)
{}
rect::rect(int w, int h, int x, int y, const std::string &image_path):
_w(w), _h(h), _x(x), _y(y)
{
auto surface = IMG_Load(image_path.c_str());
if (surface == 0)
{
std::cerr<<"FALLO AL CREAR LA SUPERFICIE\n";
}
_bazooka_texture = SDL_CreateTextureFromSurface(window::_renderer, surface);
if (!_bazooka_texture)
{
std::cerr << "FALLO AL CREAR TEXTURA.\n";
}
SDL_FreeSurface(surface);
}
rect::~rect()
{
SDL_DestroyTexture(_bazooka_texture);
}

void rect:: draw()const


{
SDL_Rect rect = { _x,_y,_w,_h };
if (_bazooka_texture)
{
SDL_RenderCopy(window::_renderer, _bazooka_texture, nullptr, &rect);
}
else
{
SDL_SetRenderDrawColor(window::_renderer, _r, _g, _b, _a);
SDL_RenderFillRect(window::_renderer, &rect);
}

void rect::pollEvents(SDL_Event &event)


{
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case (SDLK_LEFT):

PlaySound(TEXT("E:\\ORDENADOR TORRE\\UNIVERSIDAD\\3º\\INFORMÁTICA
INDUSTRIAL\\TRABAJO\\TRABAJO\\TEXTURES_PROJECT\\TEXTURES_C++\\TEXTURES_C++\\res\\SOUND\\EFFECTS\\WALKING.wav"), NULL, SND_FILENAME | SND_ASYNC);
_x = _x - 50;
break;
case (SDLK_RIGHT):
PlaySound(TEXT("E:\\ORDENADOR TORRE\\UNIVERSIDAD\\3º\\INFORMÁTICA
INDUSTRIAL\\TRABAJO\\TRABAJO\\TEXTURES_PROJECT\\TEXTURES_C++\\TEXTURES_C++\\res\\SOUND\\EFFECTS\\WALKING.wav"), NULL, SND_FILENAME | SND_ASYNC);
_x = _x + 50;
break;
case (SDLK_UP):
PlaySound(TEXT("E:\\ORDENADOR TORRE\\UNIVERSIDAD\\3º\\INFORMÁTICA
INDUSTRIAL\\TRABAJO\\TRABAJO\\TEXTURES_PROJECT\\TEXTURES_C++\\TEXTURES_C++\\res\\SOUND\\EFFECTS\\WALKING.wav"), NULL, SND_FILENAME | SND_ASYNC);
_y = _y - 50;
break;
case (SDLK_DOWN):
PlaySound(TEXT("E:\\ORDENADOR TORRE\\UNIVERSIDAD\\3º\\INFORMÁTICA
INDUSTRIAL\\TRABAJO\\TRABAJO\\TEXTURES_PROJECT\\TEXTURES_C++\\TEXTURES_C++\\res\\SOUND\\EFFECTS\\WALKING.wav"), NULL, SND_FILENAME | SND_ASYNC);
_y = _y + 50;
break;
case(SDLK_ESCAPE):
break;
}

}
if (event.type == SDL_MOUSEMOTION)
{
_x = event.motion.x;
_y = event.motion.y;
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDL_MOUSEBUTTONDOWN:
{
_x = _x;
_y = _y;
break;
}
default:
break;
}
}
}

}
#include"text.h"
#include<iostream>
#include"window.h"
text::text(SDL_Renderer *_renderer, const std::string &font_path,
int font_size,
const std::string &message_text,
const SDL_Color &color)
{
text_texture = loadFont(_renderer,font_path, font_size, message_text, color);
SDL_QueryTexture(text_texture, nullptr, nullptr,&_text_rect.w, &_text_rect.h);
}

void text::display(int x, int y, SDL_Renderer *_renderer)const


{
_text_rect.x = x;
_text_rect.y = y;
SDL_RenderCopy(_renderer, text_texture, nullptr, &_text_rect);
}

SDL_Texture *text::loadFont(SDL_Renderer *_renderer,const std::string &font_path, int font_size, const std::string &message_text, const SDL_Color
&color)
{
TTF_Font *font = TTF_OpenFont(font_path.c_str(), font_size);
if (!font)
{
std::cerr << "FALLO AL CARGAR FUENTE.\n";
}
auto text_surface = TTF_RenderText_Solid(font, message_text.c_str(),color);
if (!text_surface)
{
std::cerr << "FALLO AL CREAR SUPERFICIE PARA FUENTE.\n";
}
auto text_texture = SDL_CreateTextureFromSurface(_renderer, text_surface);
if (!text_texture)
{
std::cerr << "FALLO AL CREAR TEXTURA.\n";
}
SDL_FreeSurface(text_surface);
return text_texture;
}

#pragma once

#include<SDL_ttf.h>
#include<SDL.h>
#include<string>

class text
{
public:
text(SDL_Renderer *_renderer, const std::string &font_path, int font_size, const std::string &message_text, const SDL_Color &color);
void display(int x, int y, SDL_Renderer *_renderer)const;
static SDL_Texture *loadFont(SDL_Renderer *_renderer, const std::string &font_path, int font_size, const std::string &message_text,
const SDL_Color &color);
private:
SDL_Texture*text_texture = nullptr;
mutable SDL_Rect _text_rect;
};

MAIN ICON "icono.ico"

You might also like