78 lines
1.5 KiB
C++
78 lines
1.5 KiB
C++
#include "ball.hpp"
|
|
#include <algorithm>
|
|
#include <ranges>
|
|
#include <iostream>
|
|
|
|
inline int clamp_int(int v, int lo, int hi) {
|
|
if (v < lo) return lo;
|
|
if (v > hi) return hi;
|
|
return v;
|
|
}
|
|
|
|
|
|
ball::ball()
|
|
{
|
|
std::cout << "Ball erzeugt!"<< std::endl;
|
|
|
|
r_dir = 0;
|
|
g_dir = -1;
|
|
b_dir = 1;
|
|
|
|
x = 50;
|
|
y = 50;
|
|
|
|
rect = {20,20,20,20};
|
|
color = {255,0,0,255};
|
|
}
|
|
|
|
ball::~ball()
|
|
{
|
|
}
|
|
|
|
int ball::GetXPos(){
|
|
return x;
|
|
}
|
|
|
|
int ball::GetYPos(){
|
|
return y;
|
|
}
|
|
|
|
void ball::Draw(SDL_Renderer* renderer){
|
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
}
|
|
|
|
void ball::Update(){
|
|
color.r += r_dir;
|
|
color.g += g_dir;
|
|
color.b += b_dir;
|
|
|
|
//=================ColorCycle======================
|
|
// Clamp values between 1 and 254
|
|
color.g = clamp_int(color.g, 1, 254);
|
|
color.b = clamp_int(color.b, 1, 254);
|
|
color.r = clamp_int(color.r, 1, 254);
|
|
|
|
// Transition logic
|
|
if (r_dir != 0 && (color.r == 254 || color.r == 1)) {
|
|
r_dir = 0;
|
|
g_dir = (color.r == 254) ? 1 : -1;
|
|
b_dir = 0;
|
|
}
|
|
else if (g_dir != 0 && (color.g == 254 || color.g == 1)) {
|
|
g_dir = 0;
|
|
b_dir = (color.g == 254) ? 1 : -1;
|
|
r_dir = (color.g == 254) ? -1 : 1;
|
|
}
|
|
else if (b_dir != 0 && (color.b == 254 || color.b == 1)) {
|
|
b_dir = 0;
|
|
r_dir = 0;
|
|
g_dir = (color.b == 254) ? -1 : 1;
|
|
}
|
|
|
|
//=================Movement======================
|
|
x+=10.0;
|
|
y+=10.0;
|
|
|
|
}
|