示例#1
0
int main(int argc, char *argv[])
{
#ifdef DEBUG
    // Debugger
    QCoreApplication a(argc, argv);
#endif

    // Init
    initAll();

    // Shake Random
    srand(time(0));

#ifdef WINDOWS
    // Change lang
    PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, 2, 0);
#endif

    // Set time of out screensaver
    timerScreensaver = timer(5000);

    // Off deleting saves mode
    deleteSave = false;

    // Clear selectedGMap
    selectedSaveSlot = -1;

    // Read save
    loadSaves();

    // Set Scene
    setScene(scene["screensaver"]);

    // Start Game

    cpu = clock();
    do
    {
        // Enum CPU
        cpu = clock() - cpu;

        if(selectedScene == scene["screensaver"])
        {
            if(clock() > timerScreensaver)
            {
                setScene(scene["main"]);
            }
        }

        // Set old Width / Height
        scrOldWidth = screenWidth();
        scrOldHeight = screenHeight();

        // Draw
        update();

        // Set CPU
        cpu = clock();

        // Get char
        keyStroke = getch();
        keyChar = char(keyStroke);

        // Keys Commands
        if(selectedScene != scene["savesNew"])
        {
            keysCommand(keyStroke);
        }
        else
        {
            if(keyStroke != -1)
            {
                if(keyBackspace(keyStroke))
                {
                    if(!selectedScene->label[1]->getText().empty())
                    {
                        // Backspace
                        string newText = selectedScene->label[1]->getText();
                        newText.pop_back();

                        selectedScene->label[1]->setText(newText);
                    }
                }
                else if(keyEnter(keyStroke))
                {
                    // Create save
                    saveSlot[selectedSaveSlot] = new SaveSlot(new GMapWorld(biome["forest"]), selectedScene->label[1]->getText());
                    saveSlot[selectedSaveSlot]->gMap.emptyGMapHome = new GMapHome(biome["home"]);

                    // To game

                    setScene(scene["game"]);
                }
                else
                {
                    // Valid keyChar?
                    if(selectedScene->label[1]->font->image.find(keyChar) !=
                       selectedScene->label[1]->font->image.end())
                    {
                        // Add keyChar
                        string newText = selectedScene->label[1]->getText();
                        newText.push_back(keyChar);

                        selectedScene->label[1]->setText(newText);
                    }
                }
            }
        }
    }
    while(true);

    endwin();

    // Delete all

    // GMaps
    SAVE_DEL_MAS(saveSlot);

    // Messages
    SAVE_DEL_MAS(message);

    return 0;
}
示例#2
0
void buttonClick()
{
    // Button click

    // --------------------------- Main ---------------------------

    if(selectedScene == scene["main"])
    {
        // Play
        if(selectedScene->button[selectedButton]->getName() == "Play")
        {
            setScene(scene["saves"]);
            loadSaves();
        }

        // Continue
        else if(selectedScene->button[selectedButton]->getName() == "Continue")
        {
            if(selectedSaveSlot != -1 &&
                    !saveSlot[selectedSaveSlot]->is_empty())
            {
                // Continue game
                loadSaves();
                setScene(scene["game"]);
            }
            else
            {
                message.push_back(new PushMessage("No saves!"));
            }
        }
    }

    // --------------------------- Pause ---------------------------

    else if(selectedScene == scene["gamePause"])
    {
        // Exit to main
        if(selectedScene->button[selectedButton]->getName() == "Main")
        {
            // To main
            if(saveSlot[selectedSaveSlot]->saved())
            {
                setScene(scene["main"]);
            }
            else
            {
                setScene(scene["gamePauseExit"]);
            }
        }

        // Save
        else if(selectedScene->button[selectedButton]->getName() == "Save")
        {
            saveSaves("save.txt");
        }
    }

    // --------------------------- Exit ---------------------------

    else if(selectedScene == scene["mainExit"])
    {
        // No
        if(selectedScene->button[selectedButton]->getName() == "No")
        {
            setScene(scene["main"]);
        }

        // Yes
        else if(selectedScene->button[selectedButton]->getName() == "Yes")
        {
            exit(EXIT_SUCCESS);
        }
    }

    // --------------------------- Game exit ---------------------------

    else if(selectedScene == scene["gamePauseExit"])
    {
        // Save and exit
        if(selectedScene->button[selectedButton]->getName() == "Save and exit")
        {
            saveSaves("save.txt");
            setScene(scene["main"]);
        }

        // Exit unsaved
        else if(selectedScene->button[selectedButton]->getName() == "Exit unsaved")
        {
            setScene(scene["main"]);
        }
    }

    // --------------------------- Maps ---------------------------

    else if(selectedScene == scene["saves"])
    {
        if(selectedScene->button[selectedButton]->getName() == "Delete" ||
                selectedScene->button[selectedButton]->getName() == "Cancel")
        {
            // To delete/load save
            deleteSave = !deleteSave;

            // Rename button

            // New button
            Button* newButton =  selectedScene->button[selectedButton];

            if(deleteSave)
            {
                // Set name "Cancel"
                newButton->setName("Cancel");
            }
            else
            {
                // Set name "Delete"
                newButton->setName("Cancel");
            }

            // Set
            selectedScene->button[selectedButton] = newButton;
        }
        else
        {
            // Select GMap
            selectedSaveSlot = selectedButton;

            if(!saveSlot[selectedButton]->is_empty())
            {
                if(deleteSave)
                {
                    // Delete
                    saveSlot[selectedButton] = new SaveSlot(new GMapWorld(biome["forest"], new GMapHome(biome["home"])));

                    // Message
                    message.push_back(new PushMessage("Deleted"));

                    // Button Cancel
                    selectedButton = selectedScene->buttonId["Delete"];
                    buttonClick();

                    // Update saves/buttons
                    saveSaves();
                    loadSaves();
                }
                else
                {
                    // To game
                    setScene(scene["game"]);
                    saveSlot[selectedSaveSlot]->gMap.setSaved(true);
                }
            }
            else
            {
                if(deleteSave)
                {
                    // Message "This is empty slot"
                    message.push_back(new PushMessage("This is empty slot"));
                }
                else
                {
                    // First game

                    // Clear label
                    Label *newLabel = scene["savesNew"]->label[1];
                    newLabel->setText(string());

                    scene["savesNew"]->label[1] = newLabel;

                    // Set scene
                    setScene(scene["savesNew"]);
                }
            }
        }
    }
}
// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int cmdShow){
	argv =CommandLineToArgvW(GetCommandLineW(),&argc);

	directory = getDirectory();
	srand(time(0));
#ifdef _DEBUG
	console();
#endif
	loadSaves();
	int height=768,width =1024;
	calcSizes(768-menuHeight,1024-border);

	WNDCLASSEX wc;

	//HDC hDC;
	HGLRC hRC;

	// register window class
	//definere et vindues classe og dens parametre/udsende
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	//alt tab icon + windows linie icon
	wc.hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1) );
	//look of the cursor
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	//background color
	wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_3DFACE);
	//menu bar
	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
	wc.lpszClassName = "main";
	//icon venstre top hjørne
	//følgende virker også, loader dog default 32x32 icon
	//LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
	wc.hIconSm		 = (HICON)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);
	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// create main window
	hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,
		"main", "Walking With Dinosaurs",
		WS_THICKFRAME | WS_CAPTION | WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX |WS_CLIPCHILDREN |WS_MAXIMIZEBOX,
		GetSystemMetrics(SM_CXMAXIMIZED)/2-SM_CXFIXEDFRAME/2-width/2-listWidth, GetSystemMetrics(SM_CYMAXIMIZED)/2-SM_CYFIXEDFRAME/2-height/2, 1024, 768,
		NULL, NULL, hInstance, NULL );

	//simulations window
	// register window class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	//cs_owndc svaes the cach, used for painting outside normal routine
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "blank";
	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	blank = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("blank"), "", WS_CHILD | WS_VISIBLE ,listWidth,bAreaHeight,simWidth,simHeight, hWnd,(HMENU)IDC_SIM, GetModuleHandle(NULL), NULL);

	EnableOpenGL( blank, &hDC, &hRC );

	//selection listbox
	hWndList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL|LBS_NOTIFY,0, 0, listWidth, listHeight, hWnd,  (HMENU)IDC_LISTBOX, GetModuleHandle(NULL), NULL);
	for(int i=0;i<(int)saves.size();i++){
		SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)saves.at(i)->name.c_str());
	}
	SendMessage(hWndList,LB_SETCURSEL,0,0);

	//init creature/world
	WWDPhysics = new Physics();
	readDNA(&saves.at(0)->dna,WWDPhysics);
	WWDPhysics->runSimStartUp();
	WWDPhysics->reshape(simWidth,simHeight);

	//settings area
	int row1=10,row2=35, row3 =60;
	int col1 =160,col2=col1+170,col3=500,col4=col3+170, col5=340,col6=col5+170;
	HWND hWndFitS=CreateWindowEx(NULL,TEXT("STATIC"),	"Fitness Test",WS_CHILD|WS_VISIBLE,	col1, row1, 100, 18, hWnd, (HMENU)IDC_TEST_STATIC, GetModuleHandle(NULL),	NULL);
	HWND hwndCombo = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("combobox"), "", WS_CHILD | WS_VISIBLE| CBS_DROPDOWNLIST ,col2, row1-5, 150, 24, hWnd,  (HMENU)IDC_FITNESSTYPE_COMBOBOX, GetModuleHandle(NULL), NULL);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 0, move);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Iterative Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 1, iterateMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Dwarfslayer Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 2, dwarfslayerMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Fat Loving Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 3, fatLovingMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Box Loving Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 4, boxLovingMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Jump");
	SendMessage(hwndCombo,CB_SETITEMDATA, 5, jump);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Combi");
	SendMessage(hwndCombo,CB_SETITEMDATA, 6, combi);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"None");
	SendMessage(hwndCombo,CB_SETITEMDATA, 7, none);

	SendMessage(hwndCombo,CB_SETCURSEL,0,0);
	WWDPhysics->addFitnessFunction(move,1);

	HWND hWndButton=CreateWindowEx(NULL,TEXT("BUTTON"),	"RUN", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		col1, row3, 100, 24,
		hWnd, (HMENU)IDC_RUN_BUTTON, GetModuleHandle(NULL),	NULL);

	HWND hWndResetButton=CreateWindowEx(NULL,TEXT("BUTTON"),	"Reset Simulation", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		col3, row3, 150, 24,
		hWnd, (HMENU)IDC_RESET_BUTTON, GetModuleHandle(NULL),	NULL);

	HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
	SendMessage(hWndButton,WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));

	HWND hWndNoG=CreateWindowEx(NULL,TEXT("STATIC"),"Number of Generations",WS_CHILD|WS_VISIBLE,	col1, row2, 150, 18,
		hWnd, (HMENU)IDC_NOG_STATIC, GetModuleHandle(NULL),	NULL);

	HWND hWndNoGS=CreateWindowEx(NULL,TEXT("EDIT"),	"50",WS_CHILD|WS_VISIBLE|ES_NUMBER,	col2, row2, 42, 18,
		hWnd, (HMENU)IDC_NOG_EDIT, GetModuleHandle(NULL),	NULL);

	HWND hWndPopS=CreateWindowEx(NULL,TEXT("STATIC"),	"Population Size",WS_CHILD|WS_VISIBLE,	col3, row1, 150, 18,
		hWnd, (HMENU)IDC_POP_STATIC, GetModuleHandle(NULL),	NULL);
	HWND hWndPop=CreateWindowEx(NULL,TEXT("EDIT"),	"100",WS_CHILD|WS_VISIBLE |ES_NUMBER,	col4, row1, 42, 18,
		hWnd, (HMENU)IDC_POP_EDIT, GetModuleHandle(NULL),	NULL);

	HWND hWndViewRate=CreateWindowEx(NULL,TEXT("STATIC"),	"Simulation view precision",WS_CHILD|WS_VISIBLE,	col3, row2, 200, 18,
		hWnd, (HMENU)IDC_VIEW_STATIC, GetModuleHandle(NULL),	NULL);

	HWND hWndViewRateB=CreateWindowEx(NULL,TEXT("BUTTON"),	"",WS_CHILD|WS_VISIBLE | BS_CHECKBOX,	col4, row2, 100, 18,
		hWnd, (HMENU)IDC_VIEW_CHECKBOX, GetModuleHandle(NULL),	NULL);
	SendMessage(hWndViewRateB, BM_SETCHECK, BST_CHECKED,0);
	fixedSteps=true;
	EnableWindow(hWndViewRateB,false);


	MSG msg = messageLoop(hDC, hRC);
	return msg.wParam;
}