-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (79 loc) · 2.38 KB
/
main.cpp
File metadata and controls
94 lines (79 loc) · 2.38 KB
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
#include<SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#include<iostream>
#include<list>
#include "Entity.h"
#include "Player.h"
#include "Enemy.h"
using namespace std;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_Event Event;
SDL_Texture *background,*character;
SDL_Rect rect_background,rect_character;
int main( int argc, char* args[] )
{
//Init SDL
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
return 10;
}
//Creates a SDL Window
if((window = SDL_CreateWindow("Image Loading", 100, 100, 500/*WIDTH*/, 250/*HEIGHT*/, SDL_WINDOW_RESIZABLE | SDL_RENDERER_PRESENTVSYNC)) == NULL)
{
return 20;
}
//SDL Renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED );
if (renderer == NULL)
{
std::cout << SDL_GetError() << std::endl;
return 30;
}
//Init textures
int w=0,h=0;
background = IMG_LoadTexture(renderer,"fondo.png");
SDL_QueryTexture(background, NULL, NULL, &w, &h);
rect_background.x = 0; rect_background.y = 0; rect_background.w = w; rect_background.h = h;
character = IMG_LoadTexture(renderer, "personaje.png");
SDL_QueryTexture(character, NULL, NULL, &w, &h);
rect_character.x = 0; rect_character.y = 100; rect_character.w = w; rect_character.h = h;
list<Entity*> entities;
entities.push_back(new Player(renderer));
entities.push_back(new Enemy(renderer, 100, 30));
entities.push_back(new Enemy(renderer, 250, 100));
entities.push_back(new Enemy(renderer, 300, 70));
//Main Loop
while(true)
{
while(SDL_PollEvent(&Event))
{
if(Event.type == SDL_QUIT)
{
return 0;
}
if(Event.type == SDL_KEYDOWN)
{
if(Event.key.keysym.sym == SDLK_d)
rect_character.x++;
}
}
SDL_RenderCopy(renderer, background, NULL, &rect_background);
SDL_RenderCopy(renderer, character, NULL, &rect_character);
for(list<Entity*>::iterator i = entities.begin();
i!=entities.end();
i++)
{
(*i)->logic();
}
for(list<Entity*>::iterator i = entities.begin();
i!=entities.end();
i++)
{
(*i)->draw();
}
SDL_RenderPresent(renderer);
SDL_Delay(16);
}
return 0;
}