Renderproblem behoben, Input verarbeitung, Positionsupdate
This commit is contained in:
60
Capter1/ax_funktions.hpp
Normal file
60
Capter1/ax_funktions.hpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
//Funktionsdeklaration ax_funktions.hpp
|
||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace ax{
|
||||||
|
void print_number(int x)
|
||||||
|
{
|
||||||
|
std::cout << "Die Zahl ist: " << x << std::endl;
|
||||||
|
std::cout << "Das doppelte der Zahl ist: " << x*2 << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_char(char x)
|
||||||
|
{
|
||||||
|
std::cout << x;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int sqrt(int x){
|
||||||
|
return x*x;
|
||||||
|
}
|
||||||
|
|
||||||
|
char shift(char x){
|
||||||
|
return x-1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//allgmeine Funktion Deklaration ohne Argumente oder returns für einen unbekannten Datentyp
|
||||||
|
template<typename T> std::string type_name();
|
||||||
|
|
||||||
|
//Spezialisierte Implementation von type_name() für einen entsprechenden Datentyp vgl. mit überladung
|
||||||
|
template<> std::string type_name<int>() {return "Integer";}
|
||||||
|
template<> std::string type_name<char>(){return "Char";}
|
||||||
|
template<> std::string type_name<float>(){return "Float";}
|
||||||
|
template<> std::string type_name<double>(){return "Double";}
|
||||||
|
template<> std::string type_name<bool>(){return "Bool";}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void ausgebene(T a){
|
||||||
|
std::cout << a << " ist vom Type " << type_name<T>()<< std::endl ;
|
||||||
|
}
|
||||||
|
|
||||||
|
//CallByRefference
|
||||||
|
|
||||||
|
int fn_CtUp (int &a){
|
||||||
|
a = ++a;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int clamp_int(int v, int lo, int hi) {
|
||||||
|
if (v < lo) return lo;
|
||||||
|
if (v > hi) return hi;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
Capter1/ax_game
BIN
Capter1/ax_game
Binary file not shown.
@@ -61,6 +61,16 @@ void ax_game::Input(){
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Tastatur auslesen
|
||||||
|
const Uint8 *state = SDL_GetKeyboardState(NULL);
|
||||||
|
if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false;
|
||||||
|
if(state[SDL_SCANCODE_S]) ax_ball_one.vely += 10;
|
||||||
|
if(state[SDL_SCANCODE_W]) ax_ball_one.vely -= 10;
|
||||||
|
if(state[SDL_SCANCODE_D]) ax_ball_one.velx += 10;
|
||||||
|
if(state[SDL_SCANCODE_A]) ax_ball_one.velx -= 10;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ax_game::Update(){
|
void ax_game::Update(){
|
||||||
@@ -73,12 +83,13 @@ void ax_game::Update(){
|
|||||||
ax_TickCounter = SDL_GetTicks();
|
ax_TickCounter = SDL_GetTicks();
|
||||||
|
|
||||||
// Update Ball
|
// Update Ball
|
||||||
ax_ball_one.Update();
|
ax_ball_one.Update(DeltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ax_game::Output(){
|
void ax_game::Output(){
|
||||||
SDL_SetRenderDrawColor(ax_Renderer,0,0,255,255);
|
SDL_SetRenderDrawColor(ax_Renderer,0,0,255,255); // BG Color
|
||||||
SDL_RenderClear(ax_Renderer);
|
SDL_RenderClear(ax_Renderer); //Puffer leeren
|
||||||
|
|
||||||
|
|
||||||
ax_ball_one.Draw(ax_Renderer);
|
ax_ball_one.Draw(ax_Renderer);
|
||||||
|
|
||||||
|
|||||||
BIN
Capter1/ball
BIN
Capter1/ball
Binary file not shown.
@@ -1,13 +1,6 @@
|
|||||||
#include "ball.hpp"
|
#include "ball.hpp"
|
||||||
#include <algorithm>
|
#include "ax_funktions.hpp"
|
||||||
#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()
|
ball::ball()
|
||||||
@@ -18,8 +11,8 @@ ball::ball()
|
|||||||
g_dir = -1;
|
g_dir = -1;
|
||||||
b_dir = 1;
|
b_dir = 1;
|
||||||
|
|
||||||
x = 50;
|
velx = 200;
|
||||||
y = 50;
|
vely = 200;
|
||||||
|
|
||||||
rect = {20,20,20,20};
|
rect = {20,20,20,20};
|
||||||
color = {255,0,0,255};
|
color = {255,0,0,255};
|
||||||
@@ -30,28 +23,30 @@ ball::~ball()
|
|||||||
}
|
}
|
||||||
|
|
||||||
int ball::GetXPos(){
|
int ball::GetXPos(){
|
||||||
return x;
|
return rect.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ball::GetYPos(){
|
int ball::GetYPos(){
|
||||||
return y;
|
return rect.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ball::Draw(SDL_Renderer* renderer){
|
void ball::Draw(SDL_Renderer* renderer){
|
||||||
|
rect.x;
|
||||||
|
rect.y;
|
||||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ball::Update(){
|
void ball::Update(float f_DeltaTime){
|
||||||
color.r += r_dir;
|
color.r += r_dir;
|
||||||
color.g += g_dir;
|
color.g += g_dir;
|
||||||
color.b += b_dir;
|
color.b += b_dir;
|
||||||
|
|
||||||
//=================ColorCycle======================
|
//=================ColorCycle======================
|
||||||
// Clamp values between 1 and 254
|
// Clamp values between 1 and 254
|
||||||
color.g = clamp_int(color.g, 1, 254);
|
color.g = ax::clamp_int(color.g, 1, 254);
|
||||||
color.b = clamp_int(color.b, 1, 254);
|
color.b = ax::clamp_int(color.b, 1, 254);
|
||||||
color.r = clamp_int(color.r, 1, 254);
|
color.r = ax::clamp_int(color.r, 1, 254);
|
||||||
|
|
||||||
// Transition logic
|
// Transition logic
|
||||||
if (r_dir != 0 && (color.r == 254 || color.r == 1)) {
|
if (r_dir != 0 && (color.r == 254 || color.r == 1)) {
|
||||||
@@ -71,7 +66,12 @@ void ball::Update(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
//=================Movement======================
|
//=================Movement======================
|
||||||
x+=10.0;
|
//Update Position
|
||||||
y+=10.0;
|
rect.x +=velx * f_DeltaTime;
|
||||||
|
rect.y +=vely * f_DeltaTime;
|
||||||
|
//Kollisioinstest Fenster 1024x768
|
||||||
|
if(rect.x > (1024 -20) || rect.x < 0) velx *= -0.5;
|
||||||
|
if(rect.y > (768 - 20) || rect.y < 0) vely *= -0.5;
|
||||||
|
//std::cout << velx <<"|"<< vely <<std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "SDL2/SDL.h"
|
#include "SDL2/SDL.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class ball
|
class ball
|
||||||
{
|
{
|
||||||
@@ -8,17 +9,16 @@ private:
|
|||||||
int b_dir;
|
int b_dir;
|
||||||
public:
|
public:
|
||||||
SDL_Rect rect;
|
SDL_Rect rect;
|
||||||
float x;
|
SDL_Color color;
|
||||||
float y;
|
|
||||||
float velx;
|
float velx;
|
||||||
float vely;
|
float vely;
|
||||||
SDL_Color color;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ball();
|
ball();
|
||||||
~ball();
|
~ball();
|
||||||
int GetXPos();
|
int GetXPos();
|
||||||
int GetYPos();
|
int GetYPos();
|
||||||
void Update();
|
void Update(float f_DeltaTime);
|
||||||
void Draw(SDL_Renderer* renderer);
|
void Draw(SDL_Renderer* renderer);
|
||||||
};
|
};
|
||||||
|
|||||||
BIN
Capter1/main
BIN
Capter1/main
Binary file not shown.
@@ -16,6 +16,7 @@ int main(int argc, char** argv)
|
|||||||
|
|
||||||
ax_game AXGAME;
|
ax_game AXGAME;
|
||||||
bool success = AXGAME.Init();
|
bool success = AXGAME.Init();
|
||||||
|
|
||||||
if(success){
|
if(success){
|
||||||
AXGAME.RunLoop();
|
AXGAME.RunLoop();
|
||||||
std::cout<< i++ << std::endl;
|
std::cout<< i++ << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user