Hello,
I am trying to make an OpenGL Hearts game ( like the one from windows 7 or such) but I expected some problems.
First I will show you the code that iv done untill now :
Card.h:
#ifndef Card_H
#define Card_H
//#include <GL/glut.h>
//#include <SOIL.h>
#include <stdlib.h>
class Card {
public:
Card();
void SetCard(int);
void DisplayCard(Card);
int GetType();
int GetValue();
// GLuint cardsteaxt;
// void InitTeaxt();
// int heigth;
// int width;
protected:
int value;
int cardId;
int type;
};
#endif
Card.cpp:
#include "Card.h"
#include <iostream>
#include <string>
using namespace std;
Card::Card() : value(0), cardId(0), type(0)
{//InitTeaxt();
}
/*void Card::InitTeaxt()
{
//glGetIntegerv( GL_VIEWPORT, m_viewport );
// width=(int)m_viewport;
//heigth=GL_VIEWPORT;
cout<<width<<" "<<heigth<<endl;
cardsteaxt = SOIL_load_OGL_texture
(
"cards.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
}*/
void Card::SetCard(int id)
{
if( id>=0 && id<=52)
{
value=id%13;
cardId=id;
type=id/13;
}
else
{
value=0;
cardId=0;
type=0;
}
}
int Card::GetType()
{
return type;
}
int Card::GetValue()
{
return value;
}
void Card::DisplayCard(Card Id)
{
string tip[]={"Trefla","Spade","Inima Rosie","Romb"};
string valoare[]={"As","unu","doi","trei","patru","cinci","sase","sapte","opt","noua","zece","juvete","dama","rege"};
cout<<valoare[Id.value]<<" de "<<tip[Id.type]<<endl;
}
Deck.h:
#ifndef Deck_H
#define Deck_H
#include "Card.h"
class Deck:public Card{
public:
Deck();
void ShuffleDeck();
void Print();
void SwapCards(int,int);
Card getCard();
protected:
Card cards[52];
int DeckTop;
};
#endif
Deck.cpp:
#include "Deck.h"
#include <iostream>
#include <ctime>
using namespace std;
Deck::Deck()
{
DeckTop=0;
for ( int i=0 ; i<=52 ; i++ )
{
cards[i].SetCard(i);
}
}
void Deck::ShuffleDeck()
{
int unu,doi;
time_t t;
time(&t);
srand(t);
for ( int i=0 ; i<= 100 ; i++ )
{
unu=rand()%52;
doi=rand()%52;
SwapCards(unu,doi);
}
}
void Deck::SwapCards(int first,int second)
{
Card aux=cards[first];
cards[first]=cards[second];
cards[second]=aux;
}
void Deck::Print()
{
for ( int i=0 ; i< 52; i ++ )
DisplayCard(cards[i]);
}
Card Deck::getCard()
{
DeckTop++;
return cards[DeckTop-1];
}
Player.h:
#ifndef Player_H
#define Player_H
#include "Card.h"
#include "Deck.h"
class Player : public Deck{
public:
Player(int);
void ShowHand();
Card Hand[13];
void HandArrange();
void SwapHandCard(int,int);
private:
int curhand;
};
#endif
Player.cpp:
#include "Player.h"
#include <iostream>
using namespace std;
Player::Player(int loc)
{curhand=0;}
void Player::ShowHand()
{
for( int i=0; i<13 ; i++)
DisplayCard(Hand[i]);
}
void Player::HandArrange()
{
for(int i=0 ; i<13 ; i++)
for( int j=i; j<13;j++)
{
if(Hand[i].GetType()>Hand[j].GetType())
{
SwapHandCard(i,j);
}
}
int aux=0,ramas=0;
for(int i=0 ; i<4 ; i++)
{
while(Hand[aux].GetType()==i)
{
aux++;
}
for(int j=ramas;j<aux;j++)
for(int d=j;d<aux;d++)
if(Hand[j].GetValue()>Hand[d].GetValue())
SwapHandCard(d,j);
ramas=aux;
}
}
void Player::SwapHandCard(int first, int second)
{
Card aux=Hand[first];
Hand[first]=Hand[second];
Hand[second]=aux;
}
main.cpp:
#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <SOIL.h>
#include "Player.h"
//the 3 sizes
int wheigth=768;
int wwidth=1366;
GLuint background,cardsteaxt;
void dealHand(Deck &deck,Player &player)
{
for ( int i=0; i <13 ; i++)
{
player.Hand[i]=deck.getCard();
}
}
void Tastatura ( unsigned char key , int x , int y )
{
switch ( key )
{
case 27:
exit(0);
}
}
void Initializare ( )
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
background = SOIL_load_OGL_texture
(
"background.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
/* check for an error during the load process */
if( 0 == background )
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
}
cardsteaxt=SOIL_load_OGL_texture("cards.png",SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if(cardsteaxt==0)
printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
}
void Resize ( int w , int h )
{
wheigth=h;
wwidth=w;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w / (double)h, 1.0,200.0);
}
void Deseneaza()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 1.0f, -6.0f);
GLfloat ambientLight[] = {0.2f, 0.2f, 0.2f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
GLfloat directedLight[] = {0.7f, 0.7f, 0.7f, 1.0f};
GLfloat directedLightPos[] = {-10.0f, 15.0f, 20.0f, 0.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, directedLight);
glLightfv(GL_LIGHT0, GL_POSITION, directedLightPos);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, cardsteaxt);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor4f(1.0f,1.0f,1.0f,1.0f);
glPushMatrix();
//play
glScalef(0.75f,1.0f,1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0755f,0.0f);
glVertex3f(-2.0,2.0,-14.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(2.0,2.0,-14.0f);
glTexCoord2f(0.0f,0.25f);
glVertex3f(2.0,-2.0,-14.0f);
glTexCoord2f(0.0755f,0.25f);
glVertex3f(-2.0,-2.0,-14.0f);
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, background);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f,1.0f);
glVertex3f(-15.97f,8.3f,-15.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(15.97f,8.3f,-15.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(15.97f,-9.55f,-15.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-15.97f,-9.55f,-15.0f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(wwidth,wheigth);
glutCreateWindow("Hearts");
Initializare();
glutDisplayFunc(Deseneaza);
glutKeyboardFunc(Tastatura);
glutReshapeFunc(Resize);
Deck deck;
deck.ShuffleDeck();
Player Eu(1);
dealHand(deck,Eu);
Eu.HandArrange();
Eu.ShowHand();
printf("\n");
Player p2(2);
dealHand(deck,p2);
p2.HandArrange();
p2.ShowHand();
printf("\n");
Player p3(3);
dealHand(deck,p3);
p3.HandArrange();
p3.ShowHand();
printf("\n");
Player p4(4);
dealHand(deck,p4);
p4.HandArrange();
p4.ShowHand();
printf("\n");
glutMainLoop();
return 0;
}
I know it is a lot of code so sry:P
Anyway so I would like to know this things:
1)In the card class i would like to somehow display the card on the window ( iv tryed but it gave me errors and im not sure where i went wrong ( the try is commented out )
2)When i display a card i do not want it to look like a sqare but more like a real card ( softer edges and such) so i went into photoshop and deleted the white outside part of the next file:
Cards image
After deleteing it is basicaly a png with some "no color at all " parts ( mostly the eges and the places between the cards. But when i used it in the program nothing changed ( the white parts were still there:(( )
3)After i will show the hand i will need some mouse-functions like when the mouse is over a card to do smt.Untill now ( im not really experienced with OpenGL) i use to do using like 4 ifs and seeing if the mouse is over, but that way of doing it gave me some real problems after re-sizeing the window ( also i would like to know how to display the background picture it will still be a full-window background picture even after wierd resizes)
4) While useing glVertex3f it is really fustrateing that i can't use the pixel values and i have to use some wierd floats does anyone know how can i make it so after a resize everything works as it is supposed to be?
For now i think this is enough :)
Thnx in advance.