Skip to content

Commit

Permalink
extra customer leaves if there are no open seats, removed uneccessary…
Browse files Browse the repository at this point in the history
… parameters in UpdateState(), added character animations
  • Loading branch information
Gikoskos committed May 10, 2016
1 parent 9c76f5c commit 1304406
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 79 deletions.
4 changes: 1 addition & 3 deletions barber.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "FIFOqueue.h"


#define TIMEOUT 2000



static BOOL BarberIsInitialized = FALSE;
Expand Down Expand Up @@ -49,8 +47,8 @@ static UINT CALLBACK BarberThread(LPVOID args)
return 1;
}

//BOOL done = FALSE; the barber thread doesn't finish
barber_data *barber = (barber_data*)args;
//BOOL done = FALSE;
HANDLE ReadyCustomersOrDieObj[2] = {KillAllThreadsEvt, ReadyCustomersSem};


Expand Down
3 changes: 3 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <stdlib.h>
#include <math.h>
#include <tchar.h>
#include <assert.h>

//wrappers for the wordy winapi alloc functions
#define win_free(x) HeapFree(GetProcessHeap(), 0, x)
Expand Down Expand Up @@ -68,4 +69,6 @@ if (x) {\
break;\
}

#define TIMEOUT 2000

#endif //__COMMON_H
9 changes: 8 additions & 1 deletion customer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "FIFOqueue.h"


#define TIMEOUT 2000

#define EXIT_LOOP_IF_ASSERT(x) \
if (x) {\
Expand Down Expand Up @@ -90,11 +89,19 @@ static UINT CALLBACK CustomerThread(LPVOID args)

ReleaseSemaphore(ReadyCustomersSem, 1, NULL);
BREAK_IF_TRUE(WaitForMultipleObjects(2, BarberIsReadyOrDieObj, FALSE, INFINITE) == WAIT_OBJECT_0);

/*BREAK_IF_TRUE(WaitForMultipleObjects(2, AccessFIFOOrDieObj, FALSE, INFINITE) == WAIT_OBJECT_0);
FIFOdequeue(customer_queue);
ReleaseMutex(AccessCustomerFIFOMtx);*/

SetCustomerState(customer, GETTING_HAIRCUT);

BREAK_IF_TRUE(WaitForMultipleObjects(2, BarberIsDoneOrDieObj, FALSE, INFINITE) == WAIT_OBJECT_0);
customer->haircut_success = done = TRUE;
printf("Customer %ld is done!\n", customer->queue_num);
} else {
customer->haircut_success = FALSE;
done = TRUE;
}
}
SetCustomerState(customer, CUSTOMER_DONE);
Expand Down
11 changes: 6 additions & 5 deletions draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,7 @@ static RECT GetInvalidPositionRect(void)
}

void UpdateState(LONG num_of_customers, int *customer_states, int barber_state,
BOOL *animate_customer, BOOL animate_barber, BOOL enable_animations,
BOOL barbershop_door_is_open)
BOOL enable_animations, BOOL barbershop_door_is_open)
{
//these values are used in animating the characters
static BOOL start_animating_customer[8]; //using an automatic array for now
Expand All @@ -440,16 +439,16 @@ void UpdateState(LONG num_of_customers, int *customer_states, int barber_state,
for (int i = 0; i < CUSTOMER_CHAIRS; i++) chair_occupied[i] = FALSE;

if ((num_of_customers != current_numofcustomers) && (num_of_customers >= 0)) {
//CleanupGraphics();
current_numofcustomers = num_of_customers;

if (current_numofcustomers) {
if (!customer_graphic) {
customer_graphic = win_malloc(sizeof(RECT)*current_numofcustomers);
customer_graphic = win_malloc(sizeof(RECT) * (size_t)current_numofcustomers);
} else {
customer_graphic = win_realloc(customer_graphic, sizeof(RECT)*current_numofcustomers);
customer_graphic = win_realloc(customer_graphic, sizeof(RECT) * (size_t)current_numofcustomers);
}
}
assert(customer_graphic);
}

RECT next_to_draw;
Expand Down Expand Up @@ -519,6 +518,8 @@ void UpdateState(LONG num_of_customers, int *customer_states, int barber_state,
//TODO: fix empty chair count
int next_queue_pos = 0;
for (int i = 0; i < current_numofcustomers; i++) {
if (!customer_states[i]) continue;

switch (customer_states[i]) {
case WAITTING_IN_QUEUE:
next_to_draw = GetCustomerQueuePositionRect(next_queue_pos++);
Expand Down
5 changes: 1 addition & 4 deletions draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@


void ScaleGraphics(int scaling_exp);


void DrawBufferToWindow(HWND to, backbuffer_data *from);
void DrawToBuffer(backbuffer_data *buf);
void UpdateState(LONG num_of_customers, int *customer_states, int barber_state,
BOOL *animate_customer, BOOL animate_barber, BOOL enable_animations,
BOOL barbershop_door_is_open);
BOOL enable_animations, BOOL barbershop_door_is_open);
void CleanupGraphics(void);
#endif //__DRAW_H
139 changes: 73 additions & 66 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ typedef struct _dll_func_address {
HMODULE to_load;
void (*DrawBufferToWindow)(HWND, backbuffer_data*);
void (*DrawToBuffer)(backbuffer_data*);
void (*UpdateState)(LONG, int*, int, BOOL*, BOOL, BOOL, BOOL);
void (*UpdateState)(LONG, int*, int, BOOL, BOOL);
void (*CleanupGraphics)(void);
void (*ScaleGraphics)(int);
} dll_func_address;
Expand Down Expand Up @@ -45,7 +45,8 @@ POINT resolutions[TOTAL_RESOLUTIONS] = {
//is TRUE when the game is running and false when it's not
static BOOL running = FALSE;
static HINSTANCE g_hInst;
static BOOL animations_enabled = FALSE;
static BOOL character_animations = TRUE;
static BOOL window_animation = FALSE;


/* Prototypes for functions with local scope */
Expand Down Expand Up @@ -153,42 +154,42 @@ static BOOL ResizeWindow(HWND hwnd, int new_size, int old_size)
{
if ((new_size == old_size) || !hwnd) return FALSE;

int i, j, width, height;
POINT up_to;
if (window_animation == TRUE) {
POINT up_to;
int i, j, width, height;

width = resolutions[old_size].x;
height = resolutions[old_size].y;
up_to.x = labs(resolutions[new_size].x - resolutions[old_size].x);
up_to.y = labs(resolutions[new_size].y - resolutions[old_size].y);
width = resolutions[old_size].x;
height = resolutions[old_size].y;
up_to.x = labs(resolutions[new_size].x - resolutions[old_size].x);
up_to.y = labs(resolutions[new_size].y - resolutions[old_size].y);

#ifdef _DEBUG //disable the window resizing animation in debugging versions
HDWP winnum = BeginDeferWindowPos(1);
//no idea why 'i' needs to be less or equal than 'up_to.x' and
//'j' just less than 'up_to.y', but the resolutions work correctly like this
for (i = 1, j = 1; (i <= up_to.x) || (j < up_to.y);) {
#else
for (i = 1, j = 1; (i <= up_to.x) || (j < up_to.y);) {
HDWP winnum = BeginDeferWindowPos(1);
#endif
if (!winnum) continue;
//no idea why 'i' needs to be less or equal than 'up_to.x' and
//'j' just less than 'up_to.y', but the resolutions work correctly like this
for (i = 1, j = 1; (i <= up_to.x) || (j < up_to.y);) {
HDWP winnum = BeginDeferWindowPos(1);

if (old_size < new_size) {
DeferWindowPos(winnum, hwnd, HWND_TOP, 0, 0, width + i, height + j,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
} else {
DeferWindowPos(winnum, hwnd, HWND_TOP, 0, 0, width - i, height - j,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}
if (!winnum) continue;

i += (i <= up_to.x) ? 1 : 0;
j += (j < up_to.y) ? 1 : 0;
#ifdef _DEBUG
}
EndDeferWindowPos(winnum);
#else
if (old_size < new_size) {
DeferWindowPos(winnum, hwnd, HWND_TOP, 0, 0, width + i, height + j,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
} else {
DeferWindowPos(winnum, hwnd, HWND_TOP, 0, 0, width - i, height - j,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}

i += (i <= up_to.x) ? 1 : 0;
j += (j < up_to.y) ? 1 : 0;

EndDeferWindowPos(winnum);
}
} else {
HDWP winnum = BeginDeferWindowPos(1);
DeferWindowPos(winnum, hwnd, HWND_TOP, 0, 0,
resolutions[new_size].x, resolutions[new_size].y,
SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER);
EndDeferWindowPos(winnum);
}
#endif

return TRUE;
}
Expand Down Expand Up @@ -229,7 +230,9 @@ static LRESULT CALLBACK MainWindowProcedure(HWND hwnd, UINT msg, WPARAM wParam,
{
#define IDM_EXIT 2000
#define IDM_ABOUT 2001
#define IDM_ANIMATIONS 2002
#define IDM_CHR_ANIM 2002
#define IDM_WND_ANIM 2003

#define IDM_SMALL_RES 3000
#define IDM_MEDIUM_RES 3001
#define IDM_LARGE_RES 3002
Expand Down Expand Up @@ -283,21 +286,32 @@ static LRESULT CALLBACK MainWindowProcedure(HWND hwnd, UINT msg, WPARAM wParam,
case WM_COMMAND:
{
switch (LOWORD(wParam)) {
case IDM_ANIMATIONS:
#ifdef _DEBUG
MessageBox(hwnd, TEXT("Animations don't work in debugging mode."),
TEXT("Error"), MB_OK | MB_ICONERROR);
#else
animations_enabled = !animations_enabled;
#endif
case IDM_WND_ANIM:
window_animation = !window_animation;
break;
case IDM_CHR_ANIM:
character_animations = !character_animations;
break;
case IDM_ABOUT:
MessageBox(hwnd, TEXT(" ")
TEXT("SleepingBarber Win32\n")
TEXT(" ")
TEXT("George Koskeridis (c) 2016 "),
TEXT("About"), MB_OK);
{
MSGBOXPARAMS aboutboxopts = {
.cbSize = sizeof(MSGBOXPARAMS),
.hwndOwner = hwnd,
.hInstance = g_hInst,
.lpszText = TEXT(" ")
TEXT("SleepingBarber Win32\n")
TEXT(" ")
TEXT("George Koskeridis (c) 2016 "),
.lpszCaption = TEXT("About"),
.dwStyle = MB_OK,
.lpszIcon = NULL,
.dwContextHelpId = NULL,
.lpfnMsgBoxCallback = NULL,
.dwLanguageId = LANG_ENGLISH
};
MessageBoxIndirect(&aboutboxopts);
break;
}
case IDM_EXIT:
SendMessage(hwnd, WM_CLOSE, (WPARAM)0, (LPARAM)0);
break;
Expand All @@ -318,7 +332,7 @@ static LRESULT CALLBACK MainWindowProcedure(HWND hwnd, UINT msg, WPARAM wParam,
{
ResolutionMenu = CreatePopupMenu();

AppendMenu(ResolutionMenu, MF_STRING | MF_GRAYED, 0, TEXT("Window size"));
AppendMenu(ResolutionMenu, MF_STRING | MF_GRAYED, 0, TEXT("Window resolution"));
AppendMenu(ResolutionMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(ResolutionMenu,
MF_STRING | ((curr_resolution == SMALL_WND) ? MF_CHECKED : MF_UNCHECKED),
Expand All @@ -330,11 +344,14 @@ static LRESULT CALLBACK MainWindowProcedure(HWND hwnd, UINT msg, WPARAM wParam,
MF_STRING | ((curr_resolution == LARGE_WND) ? MF_CHECKED : MF_UNCHECKED),
IDM_LARGE_RES, TEXT("High (1000x750)"));

AppendMenu(ResolutionMenu, MF_STRING | MF_GRAYED | MF_MENUBREAK, 0, TEXT("Settings"));
AppendMenu(ResolutionMenu, MF_STRING | MF_GRAYED | MF_MENUBREAK, 0, TEXT("Animations"));
AppendMenu(ResolutionMenu, MF_SEPARATOR, 0, NULL);
AppendMenu(ResolutionMenu,
MF_STRING | ((animations_enabled) ? MF_CHECKED : MF_UNCHECKED),
IDM_ANIMATIONS, TEXT("Animations"));
MF_STRING | ((character_animations) ? MF_CHECKED : MF_UNCHECKED),
IDM_CHR_ANIM, TEXT("Characters"));
AppendMenu(ResolutionMenu,
MF_STRING | ((window_animation) ? MF_CHECKED : MF_UNCHECKED),
IDM_WND_ANIM, TEXT("Window resize"));

AppendMenu(ResolutionMenu, MF_STRING | MF_GRAYED | MF_MENUBREAK, 0, TEXT("Help"));
AppendMenu(ResolutionMenu, MF_SEPARATOR, 0, NULL);
Expand Down Expand Up @@ -381,7 +398,7 @@ static BOOL LoadDrawDLL(void)
if ((drawdll_func.to_load = LoadLibrary(drawdll_loaded_fname)) != NULL) {
drawdll_func.DrawBufferToWindow = (void(*)(HWND, backbuffer_data*))GetProcAddress(drawdll_func.to_load, "DrawBufferToWindow");
drawdll_func.DrawToBuffer = (void(*)(backbuffer_data*))GetProcAddress(drawdll_func.to_load, "DrawToBuffer");
drawdll_func.UpdateState = (void(*)(LONG, int*, int, BOOL*, BOOL, BOOL, BOOL))GetProcAddress(drawdll_func.to_load, "UpdateState");
drawdll_func.UpdateState = (void(*)(LONG, int*, int, BOOL, BOOL))GetProcAddress(drawdll_func.to_load, "UpdateState");
drawdll_func.ScaleGraphics = (void(*)(int))GetProcAddress(drawdll_func.to_load, "ScaleGraphics");
drawdll_func.CleanupGraphics = (void(*)(void))GetProcAddress(drawdll_func.to_load, "CleanupGraphics");

Expand Down Expand Up @@ -463,8 +480,6 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
//drawable character and whether to animate their movement or not
int *customer_states = win_malloc(sizeof(int) * (size_t)prev_total_customers);
int barber_state = (int)GetBarberState(barber);
BOOL *animate_customer = win_malloc(sizeof(BOOL) * (size_t)prev_total_customers);
BOOL animate_barber = FALSE;


if (!ConvertWinToClientResolutions()) {
Expand Down Expand Up @@ -538,7 +553,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
HandleMessages(SBarberMainWindow.hwnd);

//we check to see if the number of customers has changed
LONG tmp = GetNumOfCustomers();
/*LONG tmp = GetNumOfCustomers();
if (tmp != prev_total_customers) {
if (tmp - prev_total_customers < 0) {//if there are less customers than before
for (; customer_array_idx <= prev_total_customers - tmp - 1; customer_array_idx++) {
Expand All @@ -555,23 +570,17 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
win_realloc(customer_states, sizeof(int) * (size_t)prev_total_customers);
win_realloc(animate_customer, sizeof(BOOL) * (size_t)prev_total_customers);
}
tmp = GetBarberState(barber);
}*/
LONG tmp = GetBarberState(barber);
if (tmp != barber_state) {
barber_state = tmp;
animate_barber = TRUE;
} else {
animate_barber = FALSE;
}

for (int i = 0; i < prev_total_customers; i++) {
if (*(customer_array + customer_array_idx + i) != NULL) {
int tmp = (int)GetCustomerState(*(customer_array + customer_array_idx + i));
if (customer_states[i] != tmp) {
customer_states[i] = tmp;
animate_customer[i] = TRUE;
} else {
animate_customer[i] = FALSE;
}
} else {
//unreachable
Expand All @@ -580,16 +589,15 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
}
#ifdef _DEBUG
if (drawdll_func.UpdateState) drawdll_func.UpdateState(prev_total_customers, customer_states,
barber_state, animate_customer,
animate_barber, animations_enabled,
barber_state, character_animations,
GetBarbershopDoorState());
#else
UpdateState(prev_total_customers, customer_states, barber_state, animate_customer,
animate_barber, animations_enabled, GetBarbershopDoorState());
UpdateState(prev_total_customers, customer_states, barber_state, character_animations, GetBarbershopDoorState());
#endif
next_game_tick += SKIP_TICKS;
loops++;
}
Sleep(1);
}
//stop the running threads
SetEvent(KillAllThreadsEvt);
Expand All @@ -603,7 +611,6 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
}
win_free(customer_array);
win_free(customer_states);
win_free(animate_customer);
deleteFIFOqueue(customer_queue, DONT_DELETE_DATA);
if (!DeleteBarber(barber)) fprintf(stderr, "Error freeing up barber resources!\n");

Expand Down

0 comments on commit 1304406

Please sign in to comment.