-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeckGUI.cpp
executable file
·56 lines (50 loc) · 2.45 KB
/
DeckGUI.cpp
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
/*
* DeckGUI.cc
*
*
* Created by Caroline Kierstead on 25/05/09.
* Copyright 2009 UW. All rights reserved.
*
*/
#include "DeckGUI.h"
#include <algorithm>
#include <iterator>
#include <string>
// Sets up an array of the Portable Network Graphics (PNG) file names that contain the necessary card images.
// The deck will load the contents into pixel buffers for later use.
const char * image_names[] = {
// Set up clubs
"img/0_0.png", "img/0_1.png", "img/0_2.png", "img/0_3.png", "img/0_4.png", "img/0_5.png", "img/0_6.png", "img/0_7.png", "img/0_8.png", "img/0_9.png", "img/0_j.png", "img/0_q.png", "img/0_k.png",
// Set up diamonds
"img/1_0.png", "img/1_1.png", "img/1_2.png", "img/1_3.png", "img/1_4.png", "img/1_5.png", "img/1_6.png", "img/1_7.png", "img/1_8.png", "img/1_9.png", "img/1_j.png", "img/1_q.png", "img/1_k.png",
// Set up hearts
"img/2_0.png", "img/2_1.png", "img/2_2.png", "img/2_3.png", "img/2_4.png", "img/2_5.png", "img/2_6.png", "img/2_7.png", "img/2_8.png", "img/2_9.png", "img/2_j.png", "img/2_q.png", "img/2_k.png",
// Set up spades
"img/3_0.png", "img/3_1.png", "img/3_2.png", "img/3_3.png", "img/3_4.png", "img/3_5.png", "img/3_6.png", "img/3_7.png", "img/3_8.png", "img/3_9.png", "img/3_j.png", "img/3_q.png", "img/3_k.png",
// Set up the back of a card for a place holder/null card
"img/nothing.png"
};
// Loads the image from the specified file name into a pixel buffer.
Glib::RefPtr<Gdk::Pixbuf> createPixbuf(const std::string & name) {
return Gdk::Pixbuf::create_from_file( name );
} // createPixbuf
DeckGUI::DeckGUI() {
// Images can only be loaded once the main window has been initialized, so cannot be done as a static
// constant array. Instead, use the STL transform algorithm to apply the method createPixbuf to every
// element in the array of image names, starting with first and ending with the last. New elements are
// added to the back of deck.
std::transform( &image_names[0], &image_names[G_N_ELEMENTS(image_names)],
std::back_inserter(deck), &createPixbuf );
} // DeckGUI::DeckGUI
DeckGUI::~DeckGUI() {
} // DeckGUI::~DeckGUI
// Returns the image for the specified card.
Glib::RefPtr<Gdk::Pixbuf> DeckGUI::getCardImage( Rank f, Suit s ) {
int index = ((int) s)*13 + ((int) f );
return deck[ index ];
} // DeckGUI::getCardImage
// Returns the image to use for the placeholder.
Glib::RefPtr<Gdk::Pixbuf> DeckGUI::getNullCardImage() {
int size = deck.size();
return deck[ size-1 ];
} // DeckGUI::getNullCardImage