Patch by Ralf Schuelke with changes/fixes by myself:

* Pick 8 random icons from the application and preference folders
* Pick 8 different icons when the game starts again


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25224 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-04-28 10:54:38 +00:00
parent db4b6bc46c
commit cb2afe5cc7
3 changed files with 103 additions and 46 deletions
+1 -3
View File
@@ -1,8 +1,6 @@
SubDir HAIKU_TOP src apps pairs ; SubDir HAIKU_TOP src apps pairs ;
SetSubDirSupportedPlatformsBeOSCompatible ; UseLibraryHeaders icon ;
UsePrivateHeaders shared ;
Application Pairs : Application Pairs :
Pairs.cpp Pairs.cpp
+101 -42
View File
@@ -2,39 +2,37 @@
* Copyright 2008, Ralf Schülke, [email protected]. All rights reserved. * Copyright 2008, Ralf Schülke, [email protected]. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "PairsView.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <iostream> #include <Alert.h>
#include <cstdlib>
#include <ctime>
#include <Application.h> #include <Application.h>
#include <Bitmap.h> #include <Bitmap.h>
#include <Button.h> #include <Button.h>
#include <Directory.h>
#include <Entry.h>
#include <FindDirectory.h>
#include <IconUtils.h>
#include <List.h>
#include <Node.h>
#include <NodeInfo.h>
#include <Path.h>
#include "PairsTopButton.h"
#include "PairsView.h"
#include "PairsGlobal.h"
#include "Pairs.h" #include "Pairs.h"
#include "PairsGlobal.h"
#include "bitmaps/appearance.h" #include "PairsTopButton.h"
#include "bitmaps/cortex.h"
#include "bitmaps/joystick.h"
#include "bitmaps/kernel.h"
#include "bitmaps/launchbox.h"
#include "bitmaps/people.h"
#include "bitmaps/teapot.h"
#include "bitmaps/tracker.h"
// TODO: support custom board sizes // TODO: support custom board sizes
PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode) PairsView::PairsView(BRect frame, const char* name, uint32 resizingMode)
: BView(frame, name, resizingMode, B_WILL_DRAW) : BView(frame, name, resizingMode, B_WILL_DRAW)
{ {
_PairsCards(); // init bitmap pointers
for (int i = 0; i < 8; i++)
fCard[i] = NULL;
CreateGameBoard(); CreateGameBoard();
_SetPairsBoard(); _SetPairsBoard();
} }
@@ -71,23 +69,88 @@ PairsView::AttachedToWindow()
void void
PairsView::_PairsCards() PairsView::_ReadRandomIcons()
{ {
// TODO: maybe read the icons only once at startup
// clean out any previous icons
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
fCard[i] = new BBitmap(BRect(0, 0, kBitmapSize - 1, kBitmapSize - 1), delete fCard[i];
B_RGBA32); fCard[i] = NULL;
} }
// TODO: read random icons from the files in /boot/beos/apps BDirectory appsDirectory;
// and /boot/beos/preferences... BDirectory prefsDirectory;
memcpy(fCard[0]->Bits(), kappearanceBits, fCard[0]->BitsLength());
memcpy(fCard[1]->Bits(), kcortexBits, fCard[1]->BitsLength()); BPath path;
memcpy(fCard[2]->Bits(), kjoystickBits, fCard[2]->BitsLength()); if (find_directory(B_BEOS_APPS_DIRECTORY, &path) == B_OK)
memcpy(fCard[3]->Bits(), kkernelBits, fCard[3]->BitsLength()); appsDirectory.SetTo(path.Path());
memcpy(fCard[4]->Bits(), klaunchboxBits, fCard[4]->BitsLength()); if (find_directory(B_BEOS_PREFERENCES_DIRECTORY, &path) == B_OK)
memcpy(fCard[5]->Bits(), kpeopleBits, fCard[5]->BitsLength()); prefsDirectory.SetTo(path.Path());
memcpy(fCard[6]->Bits(), kteapotBits, fCard[6]->BitsLength());
memcpy(fCard[7]->Bits(), ktrackerBits, fCard[7]->BitsLength()); // read vector icons from apps and prefs folder and put them
// into a BList as BBitmaps
BList bitmaps;
BEntry entry;
while (appsDirectory.GetNextEntry(&entry) == B_OK
|| prefsDirectory.GetNextEntry(&entry) == B_OK) {
BNode node(&entry);
BNodeInfo nodeInfo(&node);
if (nodeInfo.InitCheck() < B_OK)
continue;
uint8* data;
size_t size;
type_code type;
if (nodeInfo.GetIcon(&data, &size, &type) < B_OK)
continue;
if (type != B_VECTOR_ICON_TYPE) {
delete[] data;
continue;
}
BBitmap* bitmap = new BBitmap(
BRect(0, 0, kBitmapSize - 1, kBitmapSize - 1), 0, B_RGBA32);
if (BIconUtils::GetVectorIcon(data, size, bitmap) < B_OK) {
delete[] data;
delete bitmap;
continue;
}
delete[] data;
if (!bitmaps.AddItem(bitmap))
delete bitmap;
if (bitmaps.CountItems() >= 128) {
// this is enough to choose from, stop eating memory...
break;
}
}
// pick eight random bitmaps from the ones we got in the list
srand((unsigned)time(0));
for (int i = 0; i < 8; i++) {
int32 index = rand() % bitmaps.CountItems();
fCard[i] = (BBitmap*)bitmaps.RemoveItem(index);
if (fCard[i] == NULL) {
BAlert* alert = new BAlert("fatal", "Pairs did not find enough "
"vector icons in the system, it needs at least eight.",
"Oh!", NULL, NULL, B_WIDTH_FROM_WIDEST, B_STOP_ALERT);
alert->Go();
exit(1);
}
}
// delete the remaining bitmaps from the list
while (BBitmap* bitmap = (BBitmap*)bitmaps.RemoveItem(0L))
delete bitmap;
} }
@@ -96,7 +159,7 @@ PairsView::_SetPairsBoard()
{ {
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
fButtonMessage = new BMessage(kMsgCardButton); fButtonMessage = new BMessage(kMsgCardButton);
fButtonMessage->AddInt32("ButtonNum",i); fButtonMessage->AddInt32("ButtonNum", i);
int x = i % 4 * (kBitmapSize + 10) + 10; int x = i % 4 * (kBitmapSize + 10) + 10;
int y = i / 4 * (kBitmapSize + 10) + 10; int y = i / 4 * (kBitmapSize + 10) + 10;
@@ -110,9 +173,7 @@ PairsView::_SetPairsBoard()
void void
PairsView::_GenarateCardPos() PairsView::_GenarateCardPos()
{ {
// TODO: when loading random icons, the would also have to be _ReadRandomIcons();
// loaded here (or at least somewhere in the code path that creates
// a new game after one finished)
srand((unsigned)time(0)); srand((unsigned)time(0));
@@ -125,14 +186,13 @@ PairsView::_GenarateCardPos()
fRandPos[16-i] = positions[index]; fRandPos[16-i] = positions[index];
for (int j = index; j < i - 1; j++) { for (int j = index; j < i - 1; j++)
positions[j] = positions[j + 1]; positions[j] = positions[j + 1];
}
} }
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize+10) + 10; fPosX[i] = (fRandPos[i]) % 4 * (kBitmapSize + 10) + 10;
fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize+10) + 10; fPosY[i] = (fRandPos[i]) / 4 * (kBitmapSize + 10) + 10;
} }
} }
@@ -143,9 +203,8 @@ PairsView::Draw(BRect updateRect)
SetDrawingMode(B_OP_ALPHA); SetDrawingMode(B_OP_ALPHA);
// draw rand pair 1 & 2 // draw rand pair 1 & 2
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++)
DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i])); DrawBitmap(fCard[i % 8], BPoint(fPosX[i], fPosY[i]));
}
} }
+1 -1
View File
@@ -25,7 +25,7 @@ public:
private: private:
void _SetPairsBoard(); void _SetPairsBoard();
void _PairsCards(); void _ReadRandomIcons();
void _GenarateCardPos(); void _GenarateCardPos();
BMessage* fButtonMessage; BMessage* fButtonMessage;