Browse Source

Make framerate not 7500 so I can control the goddamn character

master
Josh Gordon 4 years ago
parent
commit
f543abaa9c
  1. 4
      app/action/Makefile
  2. 65
      app/action/Timer.cpp
  3. 25
      app/action/Timer.h
  4. BIN
      app/action/action
  5. 22
      app/action/main.cpp
  6. BIN
      app/action/sp.png

4
app/action/Makefile

@ -1,7 +1,7 @@
CC = clang++
FLAGS = -lSDL2 -lSDL2_image -lSDL2_gfx
EXEC = action
SRC = main.cpp Texture.cpp Character.cpp
SRC = main.cpp Texture.cpp Character.cpp Timer.cpp
all: main.cpp Texture.cpp Character.cpp
all: main.cpp Texture.cpp Character.cpp Timer.cpp
$(CC) $(FLAGS) -o $(EXEC) $(SRC)

65
app/action/Timer.cpp

@ -0,0 +1,65 @@
#include "Timer.h"
Timer::Timer() {
startTicks_ = 0;
pauseTicks_ = 0;
started_ = false;
paused_ = false;
}
void Timer::start() {
started_ = true;
paused_ = false;
startTicks_ = SDL_GetTicks();
pauseTicks_ = 0;
}
void Timer::stop() {
started_ = false;
paused_ = false;
startTicks_ = 0;
pauseTicks_ = 0;
}
void Timer::pause() {
if(started_ && !paused_) {
paused_ = true;
pauseTicks_ = SDL_GetTicks() - startTicks_;
startTicks_ = 0;
}
}
void Timer::unpause() {
if(started_ && paused_) {
paused_ = false;
startTicks_ = SDL_GetTicks() - pauseTicks_;
pauseTicks_ = 0;
}
}
Uint32 Timer::getTicks() {
Uint32 time = 0;
if(started_) {
if(paused_) {
time = pauseTicks_;
} else {
time = SDL_GetTicks() - startTicks_;
}
}
return time;
}
bool Timer::isStarted() {
return started_;
}
bool Timer::isPaused() {
return paused_;
}

25
app/action/Timer.h

@ -0,0 +1,25 @@
#pragma once
#include <SDL2/SDL.h>
class Timer {
private:
Uint32 startTicks_,pauseTicks_;
bool paused_,started_;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
Uint32 getTicks();
bool isStarted();
bool isPaused();
};

BIN
app/action/action

22
app/action/main.cpp

@ -4,6 +4,7 @@
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include "Timer.h"
#include "Texture.h"
#include "Character.h"
@ -14,6 +15,9 @@ const int S_H = 480;
const int L_W = 1920;
const int L_H = 1080;
const int MAX_FPS = 60;
const int TICKS_PER_FRAME = 1000 / MAX_FPS;
void init();
void close();
@ -21,6 +25,9 @@ void close();
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
Timer fpsTimer;
Timer capTimer;
int frames_total;
int main(int argc, char** argv) {
@ -34,8 +41,11 @@ int main(int argc, char** argv) {
SDL_Event e;
bool done = false;
while(!done) {
fpsTimer.start();
frames_total = 0;
while(!done) {
capTimer.start();
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT) {
done = true;
@ -59,6 +69,14 @@ int main(int argc, char** argv) {
SDL_RenderPresent(renderer);
float avgFPS = frames_total / (fpsTimer.getTicks() / 1000.0f);
printf("FPS: %f\n",avgFPS);
++frames_total;
int frameTicks = capTimer.getTicks();
if(frameTicks < TICKS_PER_FRAME) { //slow down there friend
SDL_Delay(TICKS_PER_FRAME-frameTicks);
}
}
@ -86,7 +104,7 @@ void init() {
window = SDL_CreateWindow("A large but ultimately finite amount of bees",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,S_W,S_H,SDL_WINDOW_SHOWN);
CHECKNULL(window);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED);
CHECKNULL(renderer);
SDL_SetRenderDrawColor(renderer,0xFF,0xFF,0xFF,0xFF); //white with 255 alpha

BIN
app/action/sp.png

Before

Width: 235  |  Height: 259  |  Size: 14 KiB

After

Width: 64  |  Height: 64  |  Size: 14 KiB

Loading…
Cancel
Save