This repository has been archived by the owner on Mar 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cpp
107 lines (78 loc) · 2.58 KB
/
Scene.cpp
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
#include "Scene.h"
#include <iostream>
#include <windows.h>
int Scene::screen_width = GetSystemMetrics(SM_CXSCREEN);
int Scene::screen_height = GetSystemMetrics(SM_CYSCREEN);
Scene::Scene()
{
}
Scene::~Scene()
{
al_destroy_bitmap(this->background);
cout << "DESKTRUKTOR ZE SCENE" << endl;
}
void Scene::setBackground(const char* backgroundBitmap)
{
this->background = load_bitmap_at_size(backgroundBitmap, this->screen_width, this->screen_height);
if (!background)
{
fprintf(stderr, "failed to load background bitmap!\n");
exit(0);
}
this->backgroundWidth = al_get_bitmap_width(this->background);
this->backgroundHeight = al_get_bitmap_height(this->background);
}
void Scene::drawBackground(Player* player)
{
if (player->getXposition() >= backgroundXPosition + backgroundWidth) {
backgroundXPosition += backgroundWidth;
}
al_draw_bitmap(this->background, backgroundXPosition - backgroundWidth, 0, NULL);
al_draw_bitmap(this->background, backgroundXPosition, 0, NULL);
al_draw_bitmap(this->background, backgroundXPosition + backgroundWidth, 0, NULL);
}
ALLEGRO_BITMAP* Scene::load_bitmap_at_size(const char* filename, int w, int h)
{
ALLEGRO_BITMAP* resized_bmp, * loaded_bmp, * prev_target;
resized_bmp = al_create_bitmap(w, h);
if (!resized_bmp) return NULL;
loaded_bmp = al_load_bitmap(filename);
if (!loaded_bmp)
{
al_destroy_bitmap(resized_bmp);
return NULL;
}
prev_target = al_get_target_bitmap();
al_set_target_bitmap(resized_bmp);
al_draw_scaled_bitmap(loaded_bmp,
0, 0, // source origin
al_get_bitmap_width(loaded_bmp), // source width
al_get_bitmap_height(loaded_bmp), // source height
0, 0, // target origin
w, h, // target dimensions
0 // flags
);
al_set_target_bitmap(prev_target);
al_destroy_bitmap(loaded_bmp);
return resized_bmp;
}
void Scene :: cameraUpdate(float x, float y, int width, int height) {
cameraPosition = -(backgroundWidth / 2) + (x + width / 2);
if (cameraPosition < 0)
cameraPosition = 0;
}
void Scene::cameraTransform(Player *player)
{
ALLEGRO_TRANSFORM camera;
cameraUpdate(player->getXposition(), player->getYposition(), player->getBitmapWidth() / player->frames, player->getBitmapHeight() / player->levels);
al_identity_transform(&camera);
al_translate_transform(&camera, -cameraPosition, 0);
al_use_transform(&camera);
}
void Scene::resetCamera()
{
ALLEGRO_TRANSFORM camera;
al_identity_transform(&camera);
al_translate_transform(&camera, 0, 0);
al_use_transform(&camera);
}