BlackJack Algorithm - c++

What is the general alogirthm for a BlackJack game ?
I'm writing one in c++ and end up having WAY too many if statements which ruin the whole thing.
The project is a win32 GUI application, and Im posting the message loop as well as the part of the program that checks the game's status
Posting the complete code will make it humongous, so here are the links to all the files:
Full Source Code
Message Loop
LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ){
switch(Msg)
{
case WM_PAINT:
{
PAINTSTRUCT PS;
HDC hDC = BeginPaint(hWnd, &PS);
You.Draw(hDC);
Dealer.Draw(hDC);
EndPaint(hWnd, &PS);
if(Bet.Enabled){
Bet.GetFocus();
Bet.Select(0,Bet.Length());
}
}
return 0;
case WM_CTLCOLORSTATIC:
{
SetBkMode((HDC)wParam, TRANSPARENT);
}
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
case WM_CREATE:
{
//Create edit control
Bet.Create(hWnd, 10, 550, 100, 25, "0");
//Create labels
char Buffer[30];
sprintf(Buffer, "%d", You.HandValue());
BetLabel.Create(hWnd, 10, 500, 100, 40, "Enter Bet Amount");
GameControls.Create(hWnd, 10, 375, 100, 40, "Game Controls");
PlayerLabels.Create(hWnd, 10, 10, 100, 20, "You:");
PlayerLabels.Create(hWnd, 10, 150, 100, 20, "Dealer:");
HandValueLabel.Create(hWnd, (int)You.x[You.cards] + 85, (int)You.y[You.cards] + 25, 100, 20, Buffer);
YourMoney.Create(hWnd, 125, 500, 100, 40, "Your money: ");
sprintf(Buffer, "%d", You.money);
MoneyValue.Create(hWnd, 125, 525, 100, 40, Buffer);
//Create buttons
Ok.Create(hWnd, 10, 600, 100, 50, "Ok");
Hit.Create(hWnd, 10, 425, 100, 50, "Hit");
Stand.Create(hWnd, 120, 425, 100, 50, "Stand");
//Select Text
Bet.Select(0,3);
}
return 0;
case WM_CLOSE:
exit(0);
break;
case WM_COMMAND:
{
switch(HIWORD(wParam))
{
case BN_CLICKED:
{
switch(LOWORD(wParam))
{
case ID_OK:
{
//Place bet
int bet = 0;
bet = StringToNumber(Bet.Text());
You.money -= bet;
char Buffer[30];
sprintf(Buffer, "%d", You.money);
MoneyValue.SetText(Buffer);
//Update the window
InvalidateRect(hWnd, 0, TRUE);
You.Bet = YES;
Ok.Disable();
Bet.Disable();
You.DealCard();
You.DealCard();
Dealer.DealCard();
Dealer.DealCard();
You.win = false;
You.bust = false;
You.playing = YES;
Dealer.win = NO;
Dealer.bust = NO;
Dealer.playing = YES;
}
break;
case ID_HIT:
{
if(You.HandValue() < 21 && You.playing && !You.bust){
//Deal a card to the player, if he hasn't won or lost
You.DealCard();
}
if((Dealer.HandValue() <= 17 || Dealer.HandValue() < You.HandValue()) && Dealer.playing)
{
Dealer.DealCard();
}
//Update Hand Value
char Buffer[30];
sprintf(Buffer, "%d", You.HandValue());
HandValueLabel.SetText(Buffer);
InvalidateRect(hWnd,0,TRUE);
MoveWindow(HandValueLabel.Handle, (int)You.x[You.cards] + 80, (int)You.y[You.cards] + 25, 100, 20, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
}
break;
case ID_STAND:
if(Hit.Enabled){
//Don't deal more cards
Hit.Disable();
You.playing = false;
}
if((Dealer.HandValue() <= 17 || Dealer.HandValue() < You.HandValue()) && Dealer.playing){
//Deal a card to the dealer
Dealer.DealCard();
}
}
StatusChecker
void ProcessStatus(HWND hWnd){
if(Dealer.HandValue() > 17){
if(Dealer.HandValue() > You.HandValue() && Dealer.playing){
Dealer.playing = false;
}
else if(!You.playing && Dealer.HandValue() <= You.HandValue() && Dealer.playing){
Dealer.DealCard();
}
}
else if(Dealer.HandValue() <= 17 && Dealer.HandValue() <= You.HandValue() && !You.playing && Dealer.playing){
Dealer.DealCard();
}
if(EvaluateStatus() != 0){
status = OVER;
}
if(EvaluateStatus() == -1){
switch(MessageBoxA(hWnd, "You Lost! \n Keep Playing ?", "You Lost!", MB_YESNO | MB_ICONQUESTION))
{
case IDYES:
Reset();
break;
case IDNO:
exit(0);
break;
}
}
else if(EvaluateStatus() == 1){
switch(MessageBoxA(hWnd, "You Won! \n Keep Playing ?", "You Won!", MB_YESNO | MB_ICONQUESTION))
{
case IDYES:
Reset();
break;
case IDNO:
exit(0);
break;
}
}
}
int EvaluateStatus()
{
if(You.HandValue() > 21 && You.playing)
{
You.bust = true;
Dealer.win = true;
You.playing = NO;
return -1;
}
if(You.HandValue() == 21)
{
You.win = true;
return 1;
}
if(Dealer.HandValue() > 21)
{
Dealer.bust = true;
You.win = true;
return 1;
}
if(Dealer.HandValue() == 21)
{
Dealer.win = true;
return -1;
}
if(!You.playing && Dealer.HandValue() > You.HandValue() &&!Dealer.bust)
{
Dealer.win = true;
return -1;
}
if(You.HandValue() > Dealer.HandValue() && (!You.playing && (!Dealer.playing || Dealer.bust)))
{
You.win = true;
return 1;
}
if(You.HandValue() == Dealer.HandValue() && (!You.playing || !Dealer.playing))
{
You.win = true;
return 1;
}

As a first step: I probably wouldn't mix game logic and your GUI logic.. separate them into different modules.
And if you only have two players, you only need a bool to decide which one won. You don't need for both of the players Bust and Won variables.

A google search revealed this PDF paper on BlackJack algorithms. I'm sure if you did some more research using Google, you could find things too.
To figure out how to work out the actual steps needing to be done to play the game, work out on paper the different steps of the game, then decide how you can create classes that represent parts of the game and functions in these classes to execute the different steps of the game.'
This could be an iterative thing, where you go through it, then create some classes, functions, etc, now you can go back and see if what you've done makes sense, improve on the design etc.
Perhaps also play the game and see what is done, how that could be translated into code.

I have modified the way the program handles messages, and am posting it as a seperate answer.
It is still broken because of a minor hiccup, but i think the general idea is solid. comments would be appreciated
cards.h
/*
* cards.h
*
* Created on: Aug 31, 2011
* Author: Viraj
*/
#ifndef CARDS_H_
#define CARDS_H_
#include <iostream>
#include <algorithm>
#define STARTED true
#define OVER false
#define YES true
#define NO false
using namespace std;
HINSTANCE hApplication;
HBITMAP Bitmap;
char Buffer[30];
class Deck{
public:
int deck[53];
Deck(){
for(int suit = 1; suit <= 4; suit++){
for(int card = 1; card <= 13; card++){
deck[((suit-1)*13)+card] = card;
}
}
deck[0] = 0;
}
};
Deck deck;
class Cards{
private:
int card_seed;
int suit_seed;
public:
int card[11];
int cards;
int value[11];
Cards(){
fill(card, card + 10, 0);
fill(value, value + 10, 0);
cards = 0;
}
void DealCard()
{
cards++;
while(!deck.deck[card[cards]]){
card_seed = rand()%13 + 1;
suit_seed = rand()%4 + 1;
card[cards] = (suit_seed - 1)*13 + card_seed;
}
deck.deck[card[cards]] = 0;
if(card_seed > 1 && card_seed < 11){
value[cards] = card_seed;
}
else if(card_seed >= 11 && card_seed <= 13){
value[cards] = 10;
}
else if(card_seed == 1){
value[cards] = 11;
}
}
};
class Player : public Cards {
public:
float x[11];
float y[11];
bool playing;
bool bust;
bool win;
int money;
Player(const char* Type){
srand(time(NULL));
playing = true;
if(Type == "Dealer"){
for(int card = 1; card <= 10; card++){
x[card] = 10 + (card-1)*75;
y[card] = 200;
}
}
else{
for(int card = 1; card <= 10; card++){
x[card] = 10 + (card-1)*75;
y[card] = 35;
money = 1000;
}
}
}
void Reset(){
cards = 0;
fill(card, card + 10, 0);
fill(value, value + 10, 0);
playing = true;
}
int HandValue(){
int sum = 0;
for(int current_card = 1; current_card <= cards; current_card++){
sum += value[current_card];
}
if(sum > 21){
for(int current_card = 1; current_card <= cards; current_card++){
if(value[current_card] == 11 && sum > 21){
value[current_card] = 1;
sum = HandValue();
break;
}
}
}
return sum;
}
void Draw(HDC hDC){
HDC MemoryDevice;
for(int CardToLoad = 1; CardToLoad <= cards; CardToLoad++){
Bitmap = LoadBitmap(hApplication, MAKEINTRESOURCE(card[CardToLoad]));
MemoryDevice = CreateCompatibleDC(hDC);
SelectObject(MemoryDevice, Bitmap);
BitBlt(hDC, (int)x[CardToLoad], (int)y[CardToLoad], 75, 100, MemoryDevice, 0, 0, SRCCOPY);
}
DeleteDC(MemoryDevice);
}
};
class CONTROL{
public:
HWND Handle;
char* ClassName;
char* Caption;
DWORD ClassStyle;
DWORD ExtendedStyle;
int x;
int y;
int Width;
int Height;
int ID;
bool Enabled;
void Create(HWND ParentHandle, int x, int y, int Width, int Height, char* Caption){
Handle = CreateWindowExA(ExtendedStyle, ClassName, Caption, ClassStyle, x, y, Width, Height, ParentHandle, (HMENU)ID, NULL, NULL);
Enabled = true;
CONTROL::Caption = Caption;
CONTROL::x = x;
CONTROL::y = y;
CONTROL::Width = Width;
CONTROL::Height = Height;
}
void GetFocus(){
SetFocus(Handle);
}
void Disable(){
EnableWindow(Handle, FALSE);
Enabled = false;
}
void Enable(){
EnableWindow(Handle, TRUE);
Enabled = true;
}
void SetText(char* String){
SetWindowTextA(Handle, String);
}
};
class BUTTON : public CONTROL {
public:
BUTTON(int Identifier){
ClassStyle = BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD;
ID = Identifier;
ClassName = "BUTTON";
ExtendedStyle = 0;
}
};
class EDIT : public CONTROL {
public:
EDIT(int Identifier){
ClassStyle = ES_NUMBER | WS_VISIBLE | ES_CENTER | WS_CHILD | ES_NOHIDESEL;
ExtendedStyle = WS_EX_CLIENTEDGE;
ID = Identifier;
ClassName = "EDIT";
}
void Select(int first, int last){
Edit_SetSel(Handle, first, last);
}
int Length(){
return Edit_GetTextLength(Handle);
}
const char* Text(){
char Buffer[Length() + 1];
Edit_GetText(Handle, (LPTSTR)&Buffer, Length() + 1);
string String = Buffer;
return String.c_str();
}
};
class STATIC : public CONTROL {
public:
STATIC() {
ClassStyle = WS_VISIBLE | SS_CENTER | WS_CHILD;
ID = 0;
ClassName = "STATIC";
ExtendedStyle = 0;
}
};
Player You("You"), Dealer("Dealer");
BUTTON Ok(ID_OK), Hit(ID_HIT), Stand(ID_STAND);
EDIT Bet(ID_BET);
STATIC BetLabel, GameControls, PlayerLabels, HandValueLabel, YourMoney, MoneyValue, Bust;
class Game{
public:
bool started;
Game(){
started = false;
}
void Stop(){
started = false;
}
void ResetGame(HWND hWnd){
//Reset Players
You.Reset();
Dealer.Reset();
//Reset Controls
Ok.Enable();
Bet.Enable();
Hit.Enable();
//Reset Hand value
sprintf(Buffer, "%d", You.HandValue());
HandValueLabel.SetText(Buffer);
//Reset deck
deck = Deck();
//Stop the game
started = false;
//Update the Display
InvalidateRect(hWnd, 0, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
}
int SendMessage(HWND hWnd, int ButtonID, unsigned int MESSAGE, int Information, char* _Information){
return ProcessMessage(hWnd, ButtonID, MESSAGE, Information, _Information);
}
int ProcessMessage(HWND hWnd, int ButtonID, unsigned int MESSAGE, int Information, char* _Information){
switch(MESSAGE)
{
case START_GAME:
//Place Bet
You.money -= Information;
started = YES;
sprintf(Buffer, "%d", You.money);
//Update Controls
MoneyValue.SetText(Buffer);
Ok.Disable();
Bet.Disable();
//Deal two cards to player and dealer
You.DealCard();
You.DealCard();
Dealer.DealCard();
Dealer.DealCard();
//Update the Hand Value Label
sprintf(Buffer, "%d", You.HandValue());
HandValueLabel.SetText(Buffer);
//Move the label
MoveWindow(HandValueLabel.Handle, (int)You.x[You.cards] + 80, (int)You.y[You.cards] + 25, 100, 20, TRUE);
//Send PAINT message
InvalidateRect(hWnd,0, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
//Check for BlackJacks
if(You.HandValue() == 21){
SendMessage(hWnd, 0, INTERNAL, YOU_WIN, "");
}
else if(Dealer.HandValue() == 21){
SendMessage(hWnd, 0, INTERNAL, DEALER_WIN, "");
}
break;
case STAND:
if(started){
Hit.Disable();
You.playing = false;
}
break;
case INTERNAL:
switch(Information)
{
case DEALER_WIN:
return MessageBoxA(hWnd, "You Loose! \n Play Again ?", "Game Over", MB_YESNO | MB_ICONINFORMATION);
case YOU_WIN:
return MessageBoxA(hWnd, "You Win! \n Keep Playing ?", "Game Over", MB_YESNO | MB_ICONINFORMATION);
}
break;
case IDLE:
if(started){
//Check for BlackJacks
if(Dealer.HandValue() == 21){
started = false;
switch(SendMessage(hWnd, 0, INTERNAL, DEALER_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
if(!You.playing && Dealer.playing && Dealer.HandValue() < You.HandValue())
Dealer.DealCard();
//Update the Window
InvalidateRect(hWnd,0, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
//Check for a Bust
if(Dealer.HandValue() > 21){
switch(SendMessage(hWnd, 0, INTERNAL, YOU_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
if(Dealer.HandValue() == 21){
switch(SendMessage(hWnd, 0, INTERNAL, YOU_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
}
break;
case HIT:
//Check to see who's still playing and deal a card to them
if(started){
if(You.playing){
You.DealCard();
//Update the Hand Value Label
sprintf(Buffer, "%d", You.HandValue());
HandValueLabel.SetText(Buffer);
//Move the label and update the screen
MoveWindow(HandValueLabel.Handle, (int)You.x[You.cards] + 80, (int)You.y[You.cards] + 25, 100, 20, TRUE);
InvalidateRect(hWnd, 0, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
//Check for a BlackJack
if(You.HandValue() == 21){
started = false;
switch(SendMessage(hWnd, 0, INTERNAL, YOU_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
//Check for a bust
if(You.HandValue() > 21){
started = false;
switch(SendMessage(hWnd, 0, INTERNAL, YOU_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
}
if(Dealer.playing){
Dealer.DealCard();
InvalidateRect(hWnd, 0, TRUE);
RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
//Check for BlackJack
if(Dealer.HandValue() == 21){
started = false;
switch(SendMessage(hWnd, 0, INTERNAL, DEALER_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
//Check for Bust
if(Dealer.HandValue() > 21){
started = false;
switch(SendMessage(hWnd, 0, INTERNAL, YOU_WIN, ""))
{
case IDYES:
ResetGame(hWnd);
return 0;
case IDNO:
exit(0);
return 0;
}
}
}
}
break;
}
return 0;
}
};
Game game;
#endif
Message Loop
switch(Msg)
{
case WM_PAINT:
{
PAINTSTRUCT PS;
HDC hDC = BeginPaint(hWnd, &PS);
You.Draw(hDC);
Dealer.Draw(hDC);
EndPaint(hWnd, &PS);
if(Bet.Enabled){
Bet.GetFocus();
Bet.Select(0,Bet.Length());
}
}
return 0;
case WM_CTLCOLORSTATIC:
{
SetBkMode((HDC)wParam, TRANSPARENT);
}
return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
case WM_CREATE:
{
//Create edit control
Bet.Create(hWnd, 10, 550, 100, 25, "0");
//Create labels
char Buffer[30];
sprintf(Buffer, "%d", You.HandValue());
BetLabel.Create(hWnd, 10, 500, 100, 40, "Enter Bet Amount");
GameControls.Create(hWnd, 10, 375, 100, 40, "Game Controls");
PlayerLabels.Create(hWnd, 10, 10, 100, 20, "You:");
PlayerLabels.Create(hWnd, 10, 175, 100, 20, "Dealer:");
HandValueLabel.Create(hWnd, (int)You.x[You.cards] + 85, 60, 100, 20, Buffer);
YourMoney.Create(hWnd, 125, 500, 100, 40, "Your money: ");
sprintf(Buffer, "%d", You.money);
MoneyValue.Create(hWnd, 125, 525, 100, 40, Buffer);
//Create buttons
Ok.Create(hWnd, 10, 600, 100, 50, "Ok");
Hit.Create(hWnd, 10, 425, 100, 50, "Hit");
Stand.Create(hWnd, 120, 425, 100, 50, "Stand");
//Select Text
Bet.Select(0,3);
}
return 0;
case WM_CLOSE:
exit(0);
break;
case WM_COMMAND:
{
switch(HIWORD(wParam))
{
case BN_CLICKED:
{
switch(LOWORD(wParam))
{
case ID_OK:
{
//Place bet
int bet = 0;
bet = StringToNumber(Bet.Text());
game.SendMessage(hWnd, ID_OK, START_GAME, bet, "");
}
break;
case ID_HIT:
{
//Tell the game that the player wants another card
game.SendMessage(hWnd, ID_HIT, HIT, 0, "");
}
break;
case ID_STAND:
game.SendMessage(hWnd, ID_STAND, STAND, 0, "");
}
}
break;
}
}
break;
default:
//Default:
game.SendMessage(hWnd, 0, IDLE, 0, "");
return DefWindowProcA(hWnd, Msg, wParam, lParam);
}
return 0;
}

Related

C++ WINAPI How can I run an event of mouse only once?

This code in my windowless framework is an area like just button,but some code I wanted to run it only once until the mouse move on the area next time.
All those code are in WM_MOUSEMOVE.
int ClickAreaPtInfo(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,int &value)
{
POINT pt;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
RECT rect;
GetClientRect(hWnd, &rect);
RECT rc = { x,y,x + sizex,y + sizey };
if (PtInRect(&rc, pt))
{
value = 1;
return -1;
}
else
{
value = 0;
return -1;
}
return -1;
}
int _CreateMouseEvent(HWND hWnd, int x, int y, int sizex, int sizey, LPARAM lParam,RUNFUN function(), const wchar_t* Panelid, const wchar_t* CtlName)
{
int val = 0;
int msg = 0;
//int ist = 0; int istprev = 0;
RECT winrc;
GetClientRect(hWnd, &winrc);
RECT rc;
RectTypeConvert(rc,x, y, sizex, sizey);
if (Panelid == PanelID)
{
int nst = 1;
//OutputDebugString(L"HOVER!\n");
msg = 1;
ClickAreaPtInfo(hWnd, x, y, sizex, sizey, lParam, val);
if (val == 1)
{
if (ClickMsg == 1) //Click(Get from WM_LBUTTONUP)
{
ClickMsg = 0;
function();
}
else
{
//It must be run for only once until the mouse leave next time,or it will lead to a lot of resource occupation
if (CtlName == L"Button") //HOVER
{
if (nst == 1)
{
HDC hdc = GetDC(hWnd);
CreateSimpleButtonEx(hWnd, hdc, x, y, sizex, sizey, UICOLOR_GREENSEA, 1, ButtonText);
ReleaseDC(hWnd, hdc);
nst = 0;
return 0;
}
else
{
nst = 0;
return 0;
}
return 0;
}
if (CtlName == L"CloseButton") ///HOVER
{
if (nst == 1)
{
HDC hdc = GetDC(hWnd);
CreateRect(hWnd, hdc, x, y, sizex, sizey, UICOLOR_PEACHRED);
PanelDrawCloseBtn(hWnd, hdc, rc.right - 40, 0, 40, 40, 12, UICOLOR_WHITE);
ReleaseDC(hWnd, hdc);
nst = 0;
return 0;
}
else
{
nst = 0;
return 0;
}
return 0;
}
else
{
return 0;
}
}
}
if (val == 0) //Leave
{
nst = 1;
InvalidateRect(hWnd, &rc, 0); //It must be run for only once until the mouse leave next time,or it will lead to a lot of resource(CPU) occupation
}
}
if (Panelid == PrevPanelID)
{
msg = 1;
}
else
{
msg = 0;
}
return 0;
}
then handle CreateMouseEvent in WM_MOUSEMOVE:
case WM_MOUSEMOVE:
{
CreateMouseEvent(hWnd, 20, 60, 140, 40, lParam, test, L"Init",L"Button");
CreateMouseEvent(hWnd, 20, 120, 140, 40, lParam, test3, L"Init", L"Button");
CreateMouseEvent(hWnd, 20, 180, 140, 40, lParam, btn3, L"Init",L"Button");
CreateMouseEvent(hWnd, rc.right - 40, 0, 40, 40, lParam, CloseWindow, L"Init",L"CloseButton");
break;
}
And I will also give a picture to this question!
http://db.vertexstudio.xyz/lnk/PanelPic/debuginf.png
any solution?thanks!
Some of your problems have already been mentioned above.
There are some variables that are not defined in your example, neither type nor value, but I can extrapolate some at least.
Also, in several places, you try to directly compare a C style string pointer (not a std::string!) to another C style string pointer.
if (Panelid == PanelID) is one place
if (CtlName == L"Button") //HOVER === is another
if (CtlName == L"CloseButton") ///HOVER another
You can use something like int _wcsnicmp( const wchar_t *string1,const wchar_t *string2, size_t count); or whatever your compiler supports. Optionally, you could just use std::wstring.
As for the click, you need to save the rect you get with GetClientRect(hWnd, &winrc); It needs persistence, ie saved outside of the function and accessible. Then compare to this rectangle and act accordingly.

General SDL2 problems. MacOS, Xode

What I'm trying to do is make an SDL program that can write special symbols to a window I created. The keyboard can change between 4 layers each registering 32 keys. I just can't figure out how to actually make something appear on the screen and have no idea what I'm doing wrong.
#include <iostream>
#include <SDL2/SDL.h>
int main(int argc, const char * argv[])
{
//Setup
bool quit = false;
SDL_Event event;
int z = 0; //Layer
int x = 0;
int y = 0;
//Init
SDL_Init(SDL_INIT_VIDEO);
SDL_Window * window = SDL_CreateWindow("TPKB", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 960, 640, SDL_WINDOW_SHOWN);
SDL_Rect rect = {x, y, 32, 32};
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
SDL_Surface * A_srf = SDL_LoadBMP("A.bmp"); SDL_Texture * A = SDL_CreateTextureFromSurface(renderer, A_srf); SDL_FreeSurface(A_srf);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
//Events
while (!quit)
{
SDL_WaitEvent(&event);
switch (event.type)
{
case SDL_QUIT:
quit = true;
break;
case SDL_KEYUP:
{
if(z == 0)
{
switch(event.key.keysym.sym)
{
//case SDLK_1: SDL_BlitSurface(A_srf, NULL, surface, srfrect); break;
case SDLK_2: SDL_RenderCopy(renderer, A, NULL, &rect); if(x == 928){x = 0; y += 32;} else{x += 32;} rect.x = x; rect.y = y; SDL_RenderClear(renderer); SDL_RenderPresent(renderer); break;
case SDLK_LEFT: z = 1; std::cout << "1"; break;
}
}
if(z == 1)
{
switch(event.key.keysym.sym)
{
case SDLK_UP: z = 0; std::cout << "0"; break;
}
}
}
}
}
//Cleanup
SDL_DestroyRenderer(renderer);
SDL_DestroyTexture(A);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
I've tried moving the render clear render present stuff around but it hasn't seemed to be activating at all.

Ncurses issue around functions which print to a window

I'm currently making a simple ncurses menu for executing terminal commands more quickly while working on projects. I'm new to the library and keep having the same issue. I've read the documentation for the relevant functions but it seems to reveal nothing. I was hoping someone more experienced would know why there's an issue.
The function which performs "make main" has been the main one causing the problems
void makeMain( WINDOW *window ) {
wrefresh(window);
mvwprintw( window, 1, 1, "test" );
wrefresh(window);
std::system("make main");
wrefresh(window);
refresh();
wmove( window, 1, 1 );
refresh();
wrefresh( window );
}
the function is run from another function
void coordinateUserInput( int max, std::vector<command> commandslist, WINDOW *output ) {
cbreak();
noecho();
wrefresh(stdscr);
//prep work
char key_pressed;
bool can_go_up;
bool can_go_down;
int y;
int x;
const int &start_line = 3;
const int &finish_line = commandslist.size() + 2;
bool keep_going{1};
//
wmove( stdscr, 3, 2 );
refresh();
while(keep_going) {
getyx( stdscr, y, x );
can_go_up = (!(y == start_line));
can_go_down = (!(y == finish_line));
key_pressed = getch();
switch(key_pressed) {
case 'j':
if(can_go_down) {
wmove( stdscr, y + 1, 2 );
refresh();
}
break;
case 'k':
if(can_go_up) {
wmove( stdscr, y - 1, 2 );
refresh();
}
break;
case '\n':
((commandslist[y - 3]).fcnpointer)(output);
wmove( stdscr, 3, 2 );
refresh();
break;
case 'q':
keep_going = 0;
break;
default:
break;
}
}
return;
}
I'm fairly certain that these would be the problem functions. Running with -Wall and -Wextra doesn't yield errors.
int main() {
bool *is_set_up = new bool;
initscr();
refresh();
//set up command ouput window pointer
int maxx;
int *maxy = new int;
getmaxyx(stdscr, *maxy, maxx);
delete maxy;
static WINDOW *command_output = newwin( 3, maxx, 0, 0 );
box(command_output, 0, 0);
wrefresh(command_output);
//
std::vector<command> commands = {defineCommands()};
writeMenu(commands, commands.size(), is_set_up);
refresh();
coordinateUserInput(commands.size()-1, commands, command_output);
std::cout << "worked" << std::endl;
endwin();
delete is_set_up;
return 0;
}

Seg fault with Allegro bitmaps

I am using liballegro4.2-dev on Ubuntu 14.04 to (hopefully) write a simple game. My code compiles fine using the command g++ Picker.cpp -o out ``allegro-config --libs, but when I run the code I always get the error message Shutting down Allegro due to signal #11 Segmentation fault (core dumped). I suspect this has something to do with the line: masked_blit(face, screen, 0, 0, 0, 0, 64, 64);, but Internet searches turn nothing up and I am out of ideas. The full code is below:
#include <allegro.h>
void setUpAllegro()
{
//INIT
allegro_init();
install_keyboard();
install_timer();
install_mouse();
install_sound( DIGI_AUTODETECT, MIDI_AUTODETECT, 0 );
set_color_depth( 16 );
bool fullscreen = false;
if (fullscreen == true)
set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
else
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
//END INIT
}
class Player
{
public:
int x;
int y;
void Move(int dir)
{
switch(dir)
{
case 1:
y += 5;
break;
case 2:
y -= 5;
break;
case 3:
x += 5;
break;
case 4:
x -= 5;
break;
default:
//exit(1);
break;
}
}
};
int main()
{
setUpAllegro();
Player player;
bool done = false;
BITMAP *buffer = create_bitmap( 640, 480 );
BITMAP *face;
face = load_bitmap("/home/alexander/Desktop/Pooper Picker/fake.BMP",NULL);
while (!done)
{
if( key[KEY_ESC] )
done = true;
if( key[KEY_UP])
player.Move(1);
else if( key[KEY_DOWN] )
player.Move(2);
if ( key[KEY_LEFT] )
player.Move(3);
else if( key[KEY_RIGHT] )
player.Move(4);
masked_blit(face, screen, 0, 0, 0, 0, 64, 64);
blit( buffer, screen, 0, 0, 5, 5, 640, 480 );
release_screen();
clear_bitmap( buffer );
}
//free memory
destroy_bitmap( buffer );
return 0;
}
END_OF_MAIN();
Thanks for the help!

Main Menu for 2d game in ALLEGRO5

I've big problem with my university project.
I tried to make simple game in Allegro5, but I've stopped at making Main Menu.
I've basic code, but on 4 possibility: Start Game, Options, Credits,Exit. I can see just one of them ( exit ). Don't know how to fix it, can you help me ?
///////////// MENU
string menu[4] = { "Start Game", "Options", "Credits", "Exit" };
while (!quit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER)
{
redraw = true;
}
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
checked--;
break;
case ALLEGRO_KEY_DOWN:
checked++;
break;
case ALLEGRO_KEY_ESCAPE:
quit = true;
break;
case ALLEGRO_KEY_ENTER:
active = true;
break;
}
}
if (checked < 0) //JAK NAM WYJEDZIE PONIZEJ ZERA TO USTAWIAMY NA 3 (ZAPETLA SIE MENU, JAKBYS CHCIAL ZABLOKOWAC TO DAJ NA 0)
{
checked = 3;
}
if (checked > 3)
{
checked = 0;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
break;
}
**if (redraw && al_is_event_queue_empty(event_queue)) // JEZELI NIE WYKRYWA ZADNEGO RUCHU TO
{
redraw = false; // PRZERYSOWANIE WYNOSI 0
al_clear_to_color(al_map_rgb(0, 0, 0));
for (int i = 0; i < 4; i++)
{
int color;
if (i == checked)
{
color = 255;
}
else
{
color = 11;
}
al_draw_bitmap(mmenu, 0, 0, NULL);
al_draw_text(font, al_map_rgb(123, color, 45), 50, i * 60, NULL, menu[i].c_str()); // I PISZEMY TEKST
}
}
/*else if () // JEDNAK JAK ZOSTANIE WYKRYTY RUCH TO
{
redraw = true; // NASTĘPUJE PRZERYSOWANIE BITMAPY ZE ZMIENIONA OPCJA MENU
}*/**
al_flip_display();
}
al_destroy_display(display);
al_destroy_timer(timer);
al_destroy_bitmap(mmenu);
al_destroy_event_queue(event_queue);
return 0;
}