BlinkenArea - GitList
Repositories
Blog
Wiki
Blinker
Code
Commits
Branches
Tags
Search
Tree:
72fbe29
Branches
Tags
master
Blinker
src
common
Pong.cpp
pong: make ball move
Stefan Schuermans
commited
72fbe29
at 2019-06-10 12:32:31
Pong.cpp
Blame
History
Raw
/* Blinker Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org> Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html a blinkenarea.org project */ #include <stdlib.h> #include <string> #include <vector> #include <BlinkenLib/BlinkenFrame.h> #include "File.h" #include "Format.h" #include "FormatFile.h" #include "Game.h" #include "Mgrs.h" #include "Module.h" #include "OutStreamFile.h" #include "Pong.h" #include "Time.h" #include "TimeCallee.h" namespace Blinker { /** * @brief constructor * @param[in] name module name * @param[in] mgrs managers * @param[in] dirBase base directory */ Pong::Pong(const std::string &name, Mgrs &mgrs, const Directory &dirBase): Game(name, mgrs, dirBase), m_fileBallColor(dirBase.getFile("ballColor")), m_fileLineColor(dirBase.getFile("lineColor")), m_ballColor(), m_lineColor(), m_ballPosX(-1), m_ballPosY(-1), m_ballDirX(0), m_ballDirY(0) { // FIXME: activate at begin for initial development only activate(); } /// virtual destructor Pong::~Pong() { // cancel time callback request m_mgrs.m_callMgr.cancelTimeCall(this); } /// check for update of configuration (derived game), return true on update bool Pong::updateConfigGame() { bool ret = false; // color file was modified -> convert color, return true for update if (colorUpdate(m_fileBallColor, m_ballColor) || colorUpdate(m_fileLineColor, m_lineColor)) { ret = true; } return ret; } /// re-initialize game (e.g. due to config change) void Pong::reinitialize() { // TODO // convert colors color2data(m_fileBallColor, m_ballColor); color2data(m_fileLineColor, m_lineColor); // FIXME: start ball for development startBall(); } /// redraw current game image, expected to call sendFrame() at end void Pong::redraw() { int y, x; // draw background rectFill(0, m_height, 0, m_width, m_backgroundColor); // draw middle line: single line on odd width, two dashed lines at even width for (y = 0; y < m_height; ++y) { x = (m_width - (y & 1)) / 2; pixel(y, x, m_lineColor); } // draw ball pixel(m_ballPosY, m_ballPosX, m_ballColor); // send updated image buffer as frame sendFrame(); } /// callback when requested time reached void Pong::timeCall() { // bounce ball if (m_ballPosY <= 0 && m_ballDirY < 0) { m_ballDirY = 1; } if (m_ballPosY >= m_height - 1 && m_ballDirY > 0) { m_ballDirY = -1; } if (m_ballPosX <= 0 && m_ballDirX < 0) { m_ballDirX = 1; } if (m_ballPosX >= m_width - 1 && m_ballDirX > 0) { m_ballDirX = -1; } // move ball m_ballPosX += m_ballDirX; m_ballPosY += m_ballDirY; // draw and send frame redraw(); // request next call Time stepTime; stepTime.fromMs(100); m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime); } /// move ball out of the field and halt it void Pong::hideBall() { m_ballPosX = -1; m_ballPosY = -1; m_ballDirX = 0; m_ballDirY = 0; // cancel time callback request m_mgrs.m_callMgr.cancelTimeCall(this); } /// start ball void Pong::startBall() { // ball starts horizontally at middle of field, vertically random m_ballPosX = (m_width - (rand() & 1)) / 2; m_ballPosY = rand() % m_height; // random diagonal direction m_ballDirX = (rand() & 1) * 2 - 1; m_ballDirY = (rand() & 1) * 2 - 1; // request first time call Time stepTime; stepTime.fromMs(100); m_mgrs.m_callMgr.requestTimeCall(this, Time::now() + stepTime); } } // namespace Blinker