• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kgoldrunner

  • sources
  • kde-4.14
  • kdegames
  • kgoldrunner
  • src
kgrsprite.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright 2006 Mauricio Piacentini <mauricio@tabuleiro.com> *
3  * Copyright 2009 Ian Wadham <iandw.au@gmail.com> *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ****************************************************************************/
18 
19 #include "kgrsprite.h"
20 #include "kgrrenderer.h"
21 
22 #include <KDebug>
23 
24 KGrSprite::KGrSprite (KGameRenderer * renderer, QString & key,
25  const char type, const int tickTime)
26  :
27  KGameRenderedItem (renderer, key),
28 
29  m_type (type),
30  m_tickTime (tickTime),
31  m_stationary (true), // Animation is OFF at first.
32  m_x (0),
33  m_y (0),
34  m_startFrame (0),
35  m_nFrames (1),
36  m_frameCtr (0),
37  m_dx (0),
38  m_dy (0),
39  m_dt (0),
40  m_oldX (-1),
41  m_oldY (-1),
42  m_oldFrame (-1),
43  m_topLeftX (0),
44  m_topLeftY (0),
45  m_tileSize (1)
46 {
47 }
48 
49 KGrSprite::~KGrSprite()
50 {
51 }
52 
53 void KGrSprite::move (double x, double y, int frame)
54 {
55  if (frame != m_oldFrame) {
56  // Change the animation frame in KGameRenderedItem.
57  setFrame (frame);
58  m_oldFrame = frame;
59  }
60  if ((x != m_oldX) || (y != m_oldY)) {
61  // Change the position in scene (and view) coordinates.
62  setPos (m_topLeftX + (x + 1) * m_tileSize,
63  m_topLeftY + (y + 1) * m_tileSize);
64  m_oldX = x;
65  m_oldY = y;
66  }
67  return;
68 }
69 
70 void KGrSprite::setAnimation (bool repeating, int x, int y, int startFrame,
71  int nFrames, int dx, int dy, int dt, int nFrameChanges)
72 {
73  m_stationary = false; // Animation is ON now.
74  m_repeating = repeating;
75  m_x = x;
76  m_y = y;
77  m_startFrame = startFrame;
78  m_nFrames = nFrames;
79  m_frameCtr = 0;
80  m_dt = dt;
81  m_nFrameChanges = nFrameChanges;
82 
83  m_ticks = ((double) dt / m_tickTime) + 0.5;
84  m_dx = (double) dx / m_ticks;
85  m_dy = (double) dy / m_ticks;
86  m_frameTicks = (double) m_ticks / nFrameChanges;
87  m_frameChange = 0.0;
88  // kDebug() << "m_ticks" << m_ticks << "dx,dy,dt" << dx << dy << dt << "m_dx,m_dy" << m_dx << m_dy << "m_frameTicks" << m_frameTicks << "nFrames" << nFrames << "nFrameChanges" << nFrameChanges;
89 }
90 
91 void KGrSprite::animate (bool missed)
92 {
93  if (m_stationary) {
94  return;
95  }
96  if (m_frameCtr >= m_nFrames) {
97  m_frameCtr = 0;
98  if (! m_repeating) {
99  m_stationary = true; // Stop after one set of frames.
100  return;
101  }
102  }
103  // kDebug() << missed << m_frameCtr << "=" << m_x << m_y << "frame" << m_startFrame + m_frameCtr << m_frameChange;
104 
105  // If the clock is running slow, skip an animation step.
106  if (! missed) {
107  move (m_x, m_y, m_startFrame + m_frameCtr);
108  }
109 
110  // Calculate the next animation step.
111  m_frameChange = m_frameChange + 1.0;
112  if (m_frameChange + 0.001 > m_frameTicks) {
113  m_frameChange = m_frameChange - m_frameTicks;
114  m_frameCtr++;
115  }
116  m_x = m_x + m_dx;
117  m_y = m_y + m_dy;
118 }
119 
120 void KGrSprite::setCoordinateSystem (int topLeftX, int topLeftY, int tileSize)
121 {
122  if (tileSize != m_tileSize) {
123  setRenderSize (QSize (tileSize, tileSize));
124  }
125  m_tileSize = tileSize;
126  m_topLeftX = topLeftX;
127  m_topLeftY = topLeftY;
128 }
129 
130 void KGrSprite::changeCoordinateSystem(int topLeftX, int topLeftY, int tileSize)
131 {
132  setCoordinateSystem (topLeftX, topLeftY, tileSize);
133 
134  double x = m_oldX;
135  double y = m_oldY;
136  m_oldX = m_oldY = -1; // Make it look like a change of position,
137  move (x, y, m_oldFrame); // to force a recalculation of view coords.
138 }
kgrrenderer.h
KGrSprite::~KGrSprite
~KGrSprite()
Definition: kgrsprite.cpp:49
kgrsprite.h
KGrSprite::setCoordinateSystem
void setCoordinateSystem(int topLeftX, int topLeftY, int tileSize)
Definition: kgrsprite.cpp:120
QString
KGrSprite::KGrSprite
KGrSprite(KGameRenderer *renderer, QString &key, const char type, const int tickTime=20)
Definition: kgrsprite.cpp:24
KGrSprite::setAnimation
void setAnimation(bool repeating, int x, int y, int startFrame, int nFrames, int dx, int dy, int dt, int nFrameChanges)
Definition: kgrsprite.cpp:70
QSize
KGrSprite::move
void move(double x, double y, int frame)
Definition: kgrsprite.cpp:53
KGameRenderedItem
KGrSprite::animate
void animate(bool missed)
Definition: kgrsprite.cpp:91
KGrSprite::changeCoordinateSystem
void changeCoordinateSystem(int topLeftX, int topLeftY, int tileSize)
Definition: kgrsprite.cpp:130
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:24 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kgoldrunner

Skip menu "kgoldrunner"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal