File: Deck.cxx

package info (click to toggle)
oonsoo 1.1-7
  • links: PTS
  • area: non-free
  • in suites: hamm, slink
  • size: 1,556 kB
  • ctags: 428
  • sloc: cpp: 24,313; makefile: 122
file content (263 lines) | stat: -rw-r--r-- 7,494 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
///////////////////////////////////////////////////////////////////////////////
// $Id: Deck.cxx,v 1.3 1996/01/06 05:11:16 bwmott Exp $
///////////////////////////////////////////////////////////////////////////////
//
// Deck.cxx - Deck class
//
//
// Bradford W. Mott
// Copyright (C) 1994
// December 11,1994
//
///////////////////////////////////////////////////////////////////////////////
// $Log: Deck.cxx,v $
// Revision 1.3  1996/01/06 05:11:16  bwmott
// Changed all NULLs to 0
//
// Revision 1.2  1996/01/03 20:12:34  bwmott
// Fixed definitions inside case statement
//
// Revision 1.1  1995/01/12 02:10:42  bmott
// Initial revision
//
///////////////////////////////////////////////////////////////////////////////

#include <time.h>
#include <stdlib.h>

#include "UIApplication.hxx"
#include "Deck.hxx"

///////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////
Deck::Deck(ContainerWidget* parent, const char *const widgetName,
        int x, int y, int width, int maxHeight, SpriteCollection* sprites)
    : DrawingArea(parent, widgetName, x, y, width, maxHeight),
      myMaximumHeight(maxHeight),
      mySprites(sprites)
{ }

///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
Deck::~Deck()
{ }

///////////////////////////////////////////////////////////////////////////////
// Add the given card to myself
///////////////////////////////////////////////////////////////////////////////
void Deck::add(Card* card)
{
  myListOfCards.append(card);
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Add the given cards to myself
///////////////////////////////////////////////////////////////////////////////
void Deck::add(LinkedList<Card>* cards)
{
  for(Card* card = cards->removeHead(); card != 0;
       card = cards->removeHead())
  {
    myListOfCards.append(card);
  }
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Remove the my top card if one exists
///////////////////////////////////////////////////////////////////////////////
Card* Deck::remove()
{
  Card* card = myListOfCards.remove();
  updateView();
  return(card);
}

///////////////////////////////////////////////////////////////////////////////
// Turn my top card over if it needs to be
///////////////////////////////////////////////////////////////////////////////
void Deck::turnTopCardFaceUp()
{
  Card* top = myListOfCards.tail();

  if(top != 0)
  {
    top->orientation(FaceUp);
    updateView();
  }
}

///////////////////////////////////////////////////////////////////////////////
// Shuffle deck
///////////////////////////////////////////////////////////////////////////////
void Deck::shuffle()
{
  // Don't shuffle if it's empty :-)
  if(myListOfCards.size() == 0)
    return;

  // Seed the random number generator with the system time
  srand( (unsigned int)time(0) );

  for(int t=0; t<100; ++t)
  {
    int cardNumber = ((unsigned long)rand()) % myListOfCards.size();

    Card* card = myListOfCards.first();
    for(int i=0; i<cardNumber; ++i) 
      card = myListOfCards.next();

    // Move the card to the top of the deck
    myListOfCards.remove(card);
    myListOfCards.prepend(card); 
  }

  // Update my view
  updateView();
}

///////////////////////////////////////////////////////////////////////////////
// Determine the corresponding card index from the y coordinate in my view
///////////////////////////////////////////////////////////////////////////////
int Deck::cardIndexFromCoordinate(int y)
{
  int index = -1;

  if(myCardOffset > 0.0)
    index = (int)((double)y / myCardOffset);

  if(index >= myListOfCards.size())
    index = myListOfCards.size() - 1;

  return(index);
}

///////////////////////////////////////////////////////////////////////////////
// Update the graphical view of myself
///////////////////////////////////////////////////////////////////////////////
void Deck::updateView()
{
  Pixmap pixmap;
  char* imageData;

  Sprite* backOfCard = mySprites->getByName("StandardBack");
  Sprite* emptyDeck = mySprites->getByName("EmptyDeck");

  int numberOfCards = myListOfCards.size();
  int cardWidth = backOfCard->width();
  int cardHeight = backOfCard->height();
  int drawingAreaHeight;
  int drawingAreaWidth;

  // See if I have any cards
  if(numberOfCards > 0)
  {
    myCardOffset = (float)cardHeight * 0.25; 
    drawingAreaHeight = (int)(cardHeight + (numberOfCards-1) * myCardOffset);
    drawingAreaWidth = cardWidth;

    // See if we have to squash the cards to get them to fit
    if(drawingAreaHeight > myMaximumHeight)
    {
      myCardOffset = (float)(myMaximumHeight - cardHeight)/(float)(numberOfCards-1);
      drawingAreaHeight = myMaximumHeight;
    }

    // Build pixmap to hold my view
    pixmap = XCreatePixmap(application->display(), myWindow,
        drawingAreaWidth, drawingAreaHeight, 8);

    float pixmapOffset = 0.0;
    for(Card* card=myListOfCards.first(); card!=(Card*)0 ;
        card=myListOfCards.next())
    {
      Sprite* sprite;

      // See if the card is face up or face down
      if(card->orientation() == FaceDown)
        sprite = backOfCard;
      else
        sprite = card->face();

      if(card == myListOfCards.tail())
        pixmapOffset = drawingAreaHeight - cardHeight;

      XPutImage(application->display(), pixmap, application->gc(),
          sprite->image(), 0, 0, 0, (int)pixmapOffset, 
          sprite->width(), sprite->height());

      pixmapOffset += myCardOffset;
    }
  }
  else
  {
    Sprite* sprite = emptyDeck;
    drawingAreaHeight = sprite->height();
    drawingAreaWidth = sprite->width();

    // Build pixmap to hold my view
    pixmap = XCreatePixmap(application->display(), myWindow, 
        drawingAreaWidth, drawingAreaHeight, 8);
    
    XPutImage(application->display(), pixmap, application->gc(), 
        sprite->image(), 0, 0, 0, 0, sprite->width(), sprite->height());
  }

  // Install the pixmap on my drawing area
  XSetWindowBackgroundPixmap(application->display(), myWindow, pixmap);

  // Make sure the window refreshes
  XClearWindow(application->display(), myWindow);

  // Resize the window to fit the pixmap
  resize(drawingAreaWidth, drawingAreaHeight);

  // Free the pixmap
  XFreePixmap(application->display(), pixmap);
}

///////////////////////////////////////////////////////////////////////////////
// Called whenever an event arrives for me (I need to override the default)
///////////////////////////////////////////////////////////////////////////////
void Deck::handleEvent(XEvent* event)
{
  return;
  if ((event->type == ButtonPress) && (event->xbutton.button == Button1))
  {
    int done = 0;

    int parentRootX = 0;
    int parentRootY = 0;

    while(!done)
    {
      XEvent event;

      XNextEvent(application->display(), &event);

      switch(event.type)
      {
        case MotionNotify:
        {
          int newX = event.xmotion.x_root - parentRootX - width()/2;
          int newY = event.xmotion.y_root - parentRootY - height()/2;

          move(newX, newY);
          break; 
        }

        case ButtonRelease:
          done = 1;
          break;

        default:
          application->dispatchEvent(&event);
          break;
      }
    }
  }
}