第九課 SDL計時器

2021-06-27 21:05:04 字數 4087 閱讀 3034

這一課主要講sdl的乙個ticket的應用,基本的例子就是計時器的應用,沒有什麼很複雜的東西,在上一課基礎上就是加入了sdl_getticks的應用而已。

這個是乙個計時器的乙個類

timer.h

#ifndef timer_h

#define timer_h

/** 計時器

**/class timer ;

#endif

timer.cpp

#include "timer.h"

#ifdef __cplusplus

extern "c"

#endif

timer::timer()

: mstartticks(0), mpausedticks(0), mstarted(false), mpaused(false)

void timer::start()

void timer::stop()

void timer::pause()

}void timer::unpause()

}int timer::restart()

int timer::ticks() const

return 0;

}bool timer::started() const

bool timer::paused() const

sdl關於window的乙個封裝

window08.h

#ifndef window_h

#define window_h

#include #include #include #ifdef __cplusplus

extern "c"

#endif

/** sdl window的封住

*/class window08 ;

#endif

window08.cpp

#include #include #include #include "window08.h"

#ifdef __cplusplus

extern "c"

#endif

//initialize the unique_ptr's deleters here

std::unique_ptrwindow08::mwindow

= std::unique_ptr(nullptr, sdl_destroywindow);

std::unique_ptrwindow08::mrenderer

= std::unique_ptr(nullptr, sdl_destroyrenderer);

sdl_rect window08::mbox;

window08::window08()

window08::~window08()

void window08::init(std::string title)

void window08::quit()

void window08::draw(sdl_texture *tex, sdl_rect &dstrect, sdl_rect *clip, float angle,

int xpivot, int ypivot, sdl_rendererflip flip)

; //繪製

sdl_rendercopyex(mrenderer.get(), tex, clip, &dstrect, angle, &pivot, flip);

}sdl_texture* window08::loadimage(const std::string &file)

sdl_texture* window08::rendertext(const std::string &message, const std::string &fontfile, sdl_color color, int fontsize)

void window08::clear()

void window08::present()

sdl_rect window08::box()

lesson08.cpp

#include #include #include #include #include "lesson08.h"

#include "window08.h"

#include "timer.h"

clesson08::clesson08()

clesson08::~clesson08()

int clesson08::run(int argc, char* argv)

catch (const std::runtime_error &e)

std::string path(argv[1]);

std::string filepath=path + "sourcesanspro-regular.ttf";

//定時器

timer timer;

//textures的建立

sdl_texture *msg = nullptr, *ticks = nullptr;

//文字的顏色

sdl_color white = ;

//文字大小

sdl_rect msgbox, ticksbox;

filepath=path + "sourcesanspro-regular.ttf";

msg = window08::rendertext("計時器(s開始/停止,p(暫停/更新): ", filepath, white, 30);

//setup msg dstrect

msgbox.x = 0;

msgbox.y = window08::box().h / 2;

//query w & h from texture

sdl_querytexture(msg, null, null, &msgbox.w, &msgbox.h);

//setup ticks message

//we must use a stringstream to convert int to string

std::stringstream ssticks;

ssticks << timer.ticks();

ticks = window08::rendertext(ssticks.str(), filepath, white, 30);

//clear the stream

ssticks.str("");

//setup the ticks dstrect

ticksbox.x = msgbox.w + 20;

ticksbox.y = window08::box().h / 2;

sdl_querytexture(ticks, null, null, &ticksbox.w, &ticksbox.h);

//our event structure

sdl_event e;

//for tracking if we want to quit

bool quit = false;

while (!quit)

}} //logic

//if the timer is running, update the ticks message

if (timer.started() && !timer.paused())

//rendering

window08::clear();

window08::draw(msg, msgbox);

window08::draw(ticks, ticksbox);

window08::present();

}sdl_destroytexture(msg);

sdl_destroytexture(ticks);

window08::quit();

return 0;

}

到此為止sdl2在mac,window的基礎教程已經完了,如果做過window開發的,這些基本上看一下就懂了,完全不是個事。

第九課 變數

任務與 修改 沒有定義變數r,在第四行float r1,r2之後加上r就可以 include include intmain 閱讀程式 閱讀下面的程式,在閱讀過程中,請為每乙個變數畫出乙個方框,代表對應的記憶體空間。隨著閱讀,標明變數的變化過程,達到讀懂程式的目的。include intmain 圖...

C語言第九課

主要內容 高階指標 結構體指標 一 結構體指標 指向結構體變數的指標叫做結構體指標 typedef struct student student student stu student p stu student 結構體型別的指標 型別 p 結構體指標變數 變數名 結構體訪問成員變數 示例 type...

第九課 VBO索引

到目前為止,我們在建立vbo時總是重複儲存三角形公共邊的頂點。本課將介紹索引。索引是通過索引緩衝 index buffer 來實現的頂點復用。索引緩衝儲存的是整數 每個三角形有三個整數索引,分別指向各種屬性緩衝 attribute buffer 頂點位置 顏色 uv座標 其他uv座標 法線緩衝法線等...