-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacter.cpp
More file actions
128 lines (117 loc) · 2.82 KB
/
Character.cpp
File metadata and controls
128 lines (117 loc) · 2.82 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#include"CMNFUNCTION.h"
class Character{
protected:
double v0;
SDL_Texture* character;
SDL_Rect rect;
Mix_Chunk* swoosh;
Mix_Chunk* swing;
SDL_RendererFlip flip;
double angle;
bool press;
public:
Character(){
rect.x=(SCREEN_WIDTH-72)/6;
rect.y=(SCREEN_HEIGH-50)/4;
v0=0;
character=NULL;
Mix_Chunk* swoosh=NULL;
Mix_Chunk* swing=NULL;
flip=SDL_FLIP_NONE;
angle=0.0;
press=false;
}
~Character(){
SDL_DestroyTexture(character);
audio_delete();
}
void audio_delete(){
Mix_FreeChunk(swoosh);
Mix_FreeChunk(swing);
swoosh=NULL;
swing=NULL;
}
void set_swoosh(Mix_Chunk* sample){
swoosh=sample;
}
void set_swing(Mix_Chunk* sample){
swing=sample;
}
void set_v0(int v){
this->v0=v;
}
void set_x(int x){
this->rect.x=x;
}
void set_y(int y){
this->rect.y=y;
}
int get_x(){
return rect.x;
}
int get_y(){
return rect.y;
}
bool LoadCharacter(string path,SDL_Renderer* screen){
SDL_Texture* new_texture=NULL;
SDL_Surface* loadedSur=IMG_Load(path.c_str());
if(loadedSur==NULL){
cout<<"can't upload img "<<SDL_GetError();
}
else{
SDL_SetColorKey(loadedSur,SDL_TRUE,SDL_MapRGB(loadedSur->format,COLOR_KEY_R,COLOR_KEY_G,COLOR_KEY_B));
new_texture=SDL_CreateTextureFromSurface(screen,loadedSur);
if(new_texture!=NULL){
rect.w=loadedSur->w;
rect.h=loadedSur->h;
}
SDL_FreeSurface(loadedSur);
}
character=new_texture;
return character!=NULL;
}
void vertical_move(){
if(e.type==SDL_MOUSEBUTTONDOWN){
if(press==false){
Mix_PlayChannel(-1,swing,0);
v0=-11;
rect.y+=v0+0.5*1.5;
angle-=27;
if(angle<=-45){
angle+=27;
}
press=true;
}
else{
v0+=1.25;
rect.y+=v0+1.25*0.5;
angle+=1.5;
if(angle>=20){
angle-=1.5;
}
}
}
else{
v0+=1.25;
rect.y+=v0+1.25*0.5;
angle+=1.5;
if(angle>=20){
angle-=1.5;
}
press=false;
}
}
void render_character(SDL_Renderer* des,SDL_Rect* zone,SDL_Event e){
SDL_Rect render_zone={rect.x,rect.y,rect.w,rect.h};
SDL_RenderCopyEx(des,character,zone,&render_zone,angle,NULL,flip);
}
void make_die(){
v0+=1.5;
rect.y+=v0+1.5*0.5;
angle+=1.5;
if(angle>=25){
angle-=1.5;
}
}
};