-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImgObject.cpp
More file actions
88 lines (80 loc) · 2.7 KB
/
ImgObject.cpp
File metadata and controls
88 lines (80 loc) · 2.7 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
/*
ImgObject class function definitions
*/
// Include libraries
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Image.hpp>
#include <cmath>
#include <iostream>
// Include local header files
#include "ImgObject.h"
// Create img_obj and tex_obj objects
ImgObject::ImgObject() {
// Set vars
x_pos = 240;
y_pos = 120;
sine_pos = 0;
sine_pos_adj = 0;
// Load image into base_img_obj
base_img_obj = new sf::Image;
bool isExisting = base_img_obj->loadFromFile("Sonic_the_Hedgehog-title.png");
if (!isExisting) {
std::cout << "Image failed to load.\n";
exit(1);
}
// Load image into working_img_obj
working_img_obj = new sf::Image;
isExisting = working_img_obj->loadFromFile("Sonic_the_Hedgehog-title.png");
if (!isExisting) {
std::cout << "Image failed to load.\n";
exit(1);
}
// Create new text_obj and sprite_obj and set properties
tex_obj = new sf::Texture;
isExisting = tex_obj->loadFromImage(*working_img_obj);
if (!isExisting) {
std::cout << "Image failed to load.\n";
exit(1);
}
sprite_obj = new sf::Sprite(*tex_obj);
}
// Shift pixels in image
void ImgObject::shiftPixels() {
// Nested for loop that shifts every horizontal line by 8 * sin(sine_pos)
// Can make effect vertical by swapping all .y and .x and swapping all j and
// i (whole of j + 8 * sin(sine_pos) is swapped, not just j, also in the if
// statement j + 8 * sin(sine_pos) stays)
// Also create var to hold pixel data, reset sine_pos, and add an adjustment
// to sine_pos
sf::Color prev_pixel_color;
sine_pos = 0;
sine_pos = sine_pos + sine_pos_adj;
for (unsigned int i = 0; i < base_img_obj->getSize().y; i++) {
for (unsigned int j = 0; j < base_img_obj->getSize().x; j++) {
prev_pixel_color = base_img_obj->getPixel({j, i});
if (j + 8 * sin(sine_pos) < base_img_obj->getSize().x && j + 8 * sin(sine_pos) > 0) {
working_img_obj->setPixel({(unsigned int)(j + 8 * sin(sine_pos)), i}, prev_pixel_color);
}
}
// Add a certain amount to sine_pos each line
sine_pos = sine_pos + 0.1;
}
// Add a certain amount to sine_pos_adj, basically changes speed
sine_pos_adj = sine_pos_adj + 0.1;
}
// Load modified img_obj into tex_obj and set as texture of sprite_obj to
// prepare for rendering
void ImgObject::loadImgIntoTex() {
tex_obj->loadFromImage(*working_img_obj);
sprite_obj->setTexture(*tex_obj);
}
// Render sprite_obj of ImgObject
void ImgObject::renderImgObject(sf::RenderWindow* win) {
win->draw(*sprite_obj);
}
// Set sine_pos
void ImgObject::setSinePos(double arg_sine_pos) { sine_pos = arg_sine_pos; }
// Get sine_pos
double ImgObject::getSinePos() { return sine_pos; }
// Delete all char_obj related objects
ImgObject::~ImgObject() {}