Skip to content

Commit 3df888d

Browse files
committed
Fixed elastic bounce and added some control keys
some other small fixes too :)
1 parent 308cc5d commit 3df888d

File tree

11 files changed

+307
-94
lines changed

11 files changed

+307
-94
lines changed

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#OBJS specifies which files to compile as part of the project
3-
OBJS = test.cc utility.cc lTexture.o physobj.o bezier.o stickman.o
3+
OBJS = main.cc utility.cc lTexture.o physobj.o bezier.o stickman.o vec2.o
44
#CC specifies which compiler we're using
55
CC = g++
66

@@ -15,13 +15,17 @@ LINKER_FLAGS = -lSDL2 -lSDL2_image -lSDL2_ttf
1515
OBJ_NAME = stickfight
1616

1717
#This is the target that compiles our executable
18-
stickman.o: stickman.cc bezier.o physobj.o
19-
$(CC) stickman.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o stickman.o
20-
bezier.o: bezier.cc
18+
OBJS: $(OBJS)
19+
echo done
20+
vec2.o: vec2.cc
21+
$(CC) vec2.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o vec2.o
22+
bezier.o: bezier.cc vec2.o
2123
$(CC) bezier.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o bezier.o
2224
physobj.o: physobj.cc
2325
$(CC) physobj.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o physobj.o
2426
lTexture.o: lTexture.cc
2527
$(CC) lTexture.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o lTexture.o
28+
stickman.o: stickman.cc bezier.o physobj.o
29+
$(CC) stickman.cc $(COMPILER_FLAGS) $(LINKER_FLAGS) -c -o stickman.o
2630
all : $(OBJS)
2731
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)

bezier.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void polanticlock(std::vector<vec2> & Points)
276276
{
277277
if (Points.at(0).xTo(Points.at(1))<0)
278278
{
279-
Points.crbegin
279+
Points.crbegin();
280280
}
281281
}
282282
}

main.cc

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
* Revision: none
1111
* Compiler: gcc
1212
*
13-
* Author: Finn Krein, [email protected]; Arvid Krein, [email protected]
14-
* Company: -
13+
* Author: Finn Krein, [email protected]; Arvid Krein, [email protected] Company: -
1514
*
1615
* =====================================================================================
1716
*/
1817

1918
#include <SDL2/SDL.h>
2019
#include <SDL2/SDL_image.h>
2120
#include <iostream>
22-
#include <stdio.h>
21+
#include <cstdio>
2322
#include <string>
2423
#include <cmath>
2524
#include <vector>
@@ -32,7 +31,7 @@
3231
int KASTENGROSSE = 5;
3332
const int FRAMERATE = 25;
3433

35-
int main (/*int argc, char* args[])*/)
34+
int main ()
3635
{
3736
SDL_Window* gWindow;
3837
SDL_Renderer* gRenderer;
@@ -44,12 +43,15 @@ int main (/*int argc, char* args[])*/)
4443
int mousex;
4544
int mousey;
4645
SDL_Rect drawnRect = {SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 4 ,4};
47-
Worldframe world;
48-
world.xsize = SCREEN_WIDTH;
49-
world.ysize = SCREEN_HEIGHT;
50-
world.gravFy = 0;
46+
Worldframe world(40000, 0, 0);
47+
world.size.X = SCREEN_WIDTH;
48+
world.size.Y = SCREEN_HEIGHT;
49+
world.gravF.Y = -0;
50+
std::cout << "xsize: " << world.size.X << "\n" << std::flush;
51+
std::cout << "ysize: " << world.size.Y << "\n" << std::flush;
5152
std::vector<Bezpath> vBpath;
5253
kraftpartikel tempPart;
54+
std::vector<kraftpartikel>::iterator vKpointer = world.vKPartikel.begin();
5355
while (quit!=true)
5456
{
5557
while(SDL_PollEvent(&event)!=0)
@@ -79,7 +81,45 @@ int main (/*int argc, char* args[])*/)
7981
tempPart.setX(static_cast<float>(mousex));
8082
tempPart.setY(static_cast<float>(SCREEN_HEIGHT - mousey));
8183
world.vKPartikel.push_back(tempPart);
84+
vKpointer = world.vKPartikel.begin();
8285
break;
86+
case SDLK_RIGHT:
87+
vKpointer->setxvel( vKpointer->getxvel() + 5);
88+
break;
89+
case SDLK_LEFT:
90+
vKpointer->setxvel( vKpointer->getxvel() - 5);
91+
break;
92+
case SDLK_UP:
93+
vKpointer->setyvel( vKpointer->getyvel() + 5);
94+
break;
95+
case SDLK_DOWN:
96+
vKpointer->setyvel( vKpointer->getyvel() - 5);
97+
break;
98+
case SDLK_PLUS:
99+
//vKpointer->color = {0xFF,0x00,0x00,0xFF};
100+
vKpointer+=1;
101+
//vKpointer->color = {0xFF,0x00,0x00,0xFF};
102+
break;
103+
case SDLK_MINUS:
104+
//vKpointer->color = {0xFF,0x00,0x00,0xFF};
105+
vKpointer-=1;
106+
//vKpointer->color = {0xFF,0x00,0x00,0xFF};
107+
break;
108+
case SDLK_e:
109+
std::cout << world.getEnergy() << "\n" << std::flush;
110+
break;
111+
case SDLK_t:
112+
std::cout << pow( vKpointer->getyvel(), 2 ) + pow( vKpointer->getxvel(), 2 ) << "\n" << std::flush;
113+
break;
114+
case SDLK_v:
115+
std::cout << vKpointer->vel.X << " , " << vKpointer->vel.Y << "\n" << std::flush;
116+
break;
117+
case SDLK_1:
118+
world.coulombfaktor -= 1000;
119+
break;
120+
case SDLK_2:
121+
world.coulombfaktor += 1000;
122+
break;
83123
}
84124

85125
}
@@ -92,7 +132,10 @@ int main (/*int argc, char* args[])*/)
92132
}
93133
}
94134
if (!timer.getstatus())
95-
world.iterate(1e-5);
135+
{
136+
//std::cout << timer.get() << "\n";
137+
world.iterate(1e-5);
138+
}
96139
if( timer.get() >= (1000/FRAMERATE) )
97140
{
98141
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);

main.cc.old

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* =====================================================================================
3+
*
4+
* Filename: main.cc
5+
*
6+
* Description: A simple fighting game
7+
*
8+
* Version: 1.0
9+
* Created: 21.03.2017 16:39:34
10+
* Revision: none
11+
* Compiler: gcc
12+
*
13+
* Author: Finn Krein, [email protected]; Arvid Krein, [email protected]
14+
* Company: -
15+
*
16+
* =====================================================================================
17+
*/
18+
19+
#include <SDL2/SDL.h>
20+
#include <SDL2/SDL_image.h>
21+
#include <iostream>
22+
#include <cstdio>
23+
#include <string>
24+
#include <cmath>
25+
#include <vector>
26+
#include "utility.h"
27+
#include "lTexture.h"
28+
#include "physobj.h"
29+
#include "bezier.h"
30+
#include "stickman.h"
31+
32+
int KASTENGROSSE = 5;
33+
const int FRAMERATE = 25;
34+
35+
int main ()
36+
{
37+
SDL_Window* gWindow;
38+
SDL_Renderer* gRenderer;
39+
init(&gWindow,&gRenderer, SCREEN_HEIGHT, SCREEN_WIDTH);
40+
41+
bool quit=false;
42+
SDL_Event event;
43+
simpleTimer timer;
44+
int mousex;
45+
int mousey;
46+
SDL_Rect drawnRect = {SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 4 ,4};
47+
Worldframe world;
48+
world.xsize = SCREEN_WIDTH;
49+
world.ysize = SCREEN_HEIGHT;
50+
world.gravFy = 0;
51+
std::vector<Bezpath> vBpath;
52+
kraftpartikel tempPart;
53+
while (quit!=true)
54+
{
55+
while(SDL_PollEvent(&event)!=0)
56+
{
57+
if(event.type == SDL_QUIT)
58+
quit=true;
59+
if(event.type == SDL_KEYDOWN)
60+
{
61+
switch (event.key.keysym.sym)
62+
{
63+
case SDLK_b:
64+
vBpath.push_back(createbezierpath(gRenderer));
65+
break;
66+
case SDLK_p:
67+
timer.flip();
68+
break;
69+
case SDLK_o:
70+
timer.reset();
71+
break;
72+
case SDLK_i:
73+
std::cout << timer.get() << "\n" << std::flush;
74+
break;
75+
case SDLK_w:
76+
std::cout << (world.vKPartikel.end()-1)->getX() << " , " << (world.vKPartikel.end()-1)->getY() << "\n" << std::flush;
77+
break;
78+
case SDLK_n:
79+
tempPart.setX(static_cast<float>(mousex));
80+
tempPart.setY(static_cast<float>(SCREEN_HEIGHT - mousey));
81+
world.vKPartikel.push_back(tempPart);
82+
break;
83+
}
84+
85+
}
86+
87+
if (event.type == SDL_MOUSEBUTTONDOWN)
88+
{
89+
SDL_GetMouseState(&mousex, &mousey);
90+
drawnRect.x=mousex-KASTENGROSSE/2;
91+
drawnRect.y=mousey-KASTENGROSSE/2;
92+
}
93+
}
94+
if (!timer.getstatus())
95+
world.iterate(1e-5);
96+
if( timer.get() >= (1000/FRAMERATE) )
97+
{
98+
SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
99+
SDL_RenderClear(gRenderer);
100+
SDL_SetRenderDrawColor(gRenderer, 0x00, 0x00, 0x00, 0xFF);
101+
drawobjs(gRenderer, world.vKPartikel, KASTENGROSSE);
102+
SDL_RenderDrawRect(gRenderer, &drawnRect);
103+
SDL_RenderPresent(gRenderer);
104+
timer.reset();
105+
}
106+
}
107+
108+
SDL_DestroyWindow(gWindow);
109+
SDL_DestroyRenderer(gRenderer);
110+
gWindow = NULL;
111+
gRenderer = NULL;
112+
113+
return 0;
114+
}
115+
116+

0 commit comments

Comments
 (0)