1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

i made this c++ in visual studio... how can i get it to work in xcode

Discussion in 'Mac Programming' started by DaKyd55, Apr 21, 2011.

  1. DaKyd55, Apr 21, 2011
    Last edited by a moderator: Apr 21, 2011

    macrumors member

    #1
    i made this last summer in windows visual studio express... can you alter the code so that it works in xcode?

    here is the code:



    Code:
    #include <iostream>
    #include <windows.h>
    #include <stdio.h>
    #include <ctime>
    #include <conio.h>
    #include <string>
    
    /*
    skeleton for ascii games: displays an array of ascii art, refreshing every N milliseconds(n=300 in the example); 
    also checks for  keypresses every refresh.
    */
    using namespace std;
    
    //display array:
    const int x=40, y=40;
    char display[x][y];
    int personx=20; 
    int persony=20;
    string blocks[y]={
    "           V           V          V     ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        ",
    "                                        "
    };
    
    int score=0;
    void redraw();//redraw screen
    
    int main()
    {
    	
    system("color 06");
    	srand((unsigned int)time(0));// random number seed
    
    	while (true)
    	{
    	int timestep=30;//timestep in miliseconds
    
    	//initialize screen:
    	for(int i=0; i<x; i++)
    		for(int j=0; j<y; j++)
    			display[i][j]=' ';
    
    	for(int i=0; i<x; i++)
    		for(int j=0; j<y; j++)
    			blocks[j][i]=' ';
    
    
    
    	//console size setup:
    	COORD bufferSize = {110+1, x+1};
        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize);
    	SMALL_RECT WinRect = {0, 0, 100, x};
        SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, &WinRect);
    
    	bool displayText = true;
    	while(!_kbhit()){
    		clock_t waketime=clock()+300;
    		while(clock()<waketime);//wait until taking the next step
    		system("cls");
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "    :::     ::::::::::: :::::::::   :::::::: ::::::::::: :::::::::  ::::::::::: :::    ::: :::::::::: " << endl;
    		cout << "  :+: :+:       :+:     :+:    :+: :+:    :+:    :+:     :+:    :+:     :+:     :+:   :+:  :+:        " << endl;
    		cout << " +:+   +:+      +:+     +:+    +:+ +:+           +:+     +:+    +:+     +:+     +:+  +:+   +:+        " << endl;
    		cout << "+#++:++#++:     +#+     +#++:++#:  +#++:++#++    +#+     +#++:++#:      +#+     +#++:++    +#++:++#   " << endl;
    		cout << "+#+     +#+     +#+     +#+    +#+        +#+    +#+     +#+    +#+     +#+     +#+  +#+   +#+        " << endl;
    		cout << "#+#     #+#     #+#     #+#    #+# #+#    #+#    #+#     #+#    #+#     #+#     #+#   #+#  #+#        " << endl;
    		cout << "###     ### ########### ###    ###  ########     ###     ###    ### ########### ###    ### ########## " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "                               1st player       2nd player                                            " << endl;
    		cout << "                               A --> left       J --> left      don't get hit by the missiles --> V   " << endl;
    		cout << "                               D --> right      L --> left              made by Sean Panayi           " << endl;
    		if (displayText==true)
    		{
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;
    		cout << "                                                                                                      " << endl;	
    		cout << "                             Press 1 For Single Player or 2 for Multiplayer                           " << endl;
    		}
    		displayText = !displayText;
    	}
    	bool twoplayer=true;
    	if(_getch()=='1')
    	{
    		twoplayer = false;
    	}
    	else
    	{
    		twoplayer = true;
    	}
    system ("cls");
    	cout << "                                                                                 " << endl;
    	cout << "                                                                                 " << endl;
    	cout << "                          __                            __   _ _                 " << endl;
    	cout << "                         /  |_  _  _  _ _  \\_/_     _  |  \\.(_(_. _   ||_        " << endl;
    	cout << "                         \\__| )(_)(_)_)(-   |(_)|_||   |__/|| | |(_|_|||_\\/      " << endl;
    	cout << "                                                                         /       " << endl;
    	cout << "                                                                                 " << endl;
    	cout << "                           beginner --> 1 intermediate --> 2 advanced --> 3      " << endl;
    	cout << "                                                                                 " << endl;
    	cout << "                                                                                 " << endl;
    	cout << "                                                                                 " << endl;
    	cout << "                                                                                 " << endl;
    
    	while(!kbhit){};
    	char level = _getch();
    	if (level=='1') timestep=100;
    	if (level=='2') timestep=35;
    	if (level=='3') timestep=10;
    
    
    	//some code that lets the user pick one or two player goes here.
    
    	if(twoplayer)
    	{
    		personx=1;
    		persony=39;
    	}
    	else
    	{
    		personx=20;
    	}
    
    	//console size setup:
    	COORD bufferSize2 = {x+1, y+1};
        SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize2);
    	SMALL_RECT WinRect2 = {0, 0, x, y};
        SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), true, &WinRect2);
    
    	//main loop:
    	while(true)
    	{
    		score=score+1; 
    		if (level=='1') score=score+1;
    		if (level=='2') score=score+2;
    		if (level=='3') score=score+3;
    		clock_t waketime=clock()+timestep;
    		while(clock()<waketime);//wait until taking the next step
    
    		//process keypress, if any:
    		if(_kbhit())//if there was a key press
    		{
    			char key=(char)_getch();//get which key it was
    			switch(key)
    			{
    			case 'a':
    				personx=personx-1;
    				break;
    			case 'd':
    				personx=personx+1;
    				break;
    			case 'j':
    				persony=persony-1;
    				break;
    			case 'l':
    				persony=persony+1;
    
    			}
    		}
    		//process game logic here
    
    	
    		int g=rand() % x;
    		blocks[0][g]='V';
    		for(int i=0; i<x; i++)
    			for(int j=0; j<y; j++)
    				display[i][j]=blocks[j][i];
    
    		if(display [personx][39]==' ' || display [personx][39]=='Y' )
    		{
    			display [personx][39]='T';
    		}
    		else
    		{
    			break;
    		}
    		if(twoplayer)
    		{
    			if(display [persony][39]==' ' || display [persony][39]=='T' )
    			{
    				display [persony][39]='Y';
    			}
    			else
    			{
    				break;
    			}
    		}
    
    
    		redraw();
    	
    		for (int i=y-1; i>0; i--)
    		{
    			blocks [i]=blocks [i-1];
    		}
    		blocks [0]="                                        ";
    
    	}
    	system("cls");
    	cout << "                                               " << endl;
    	cout << "                                               " << endl;
    	cout << "                                               " << endl;
    	cout << "                                               " << endl;
    	cout << "  _______      ___      .___  ___.  _______    " << endl;
    	cout << " /  _____|    /   \\     |   \\/   | |   ____|    " << endl;
    	cout << "|  |  __     /  ^  \\    |  \\  /  | |  |__       " << endl;
    	cout << "|  | |_ |   /  /_\\  \\   |  |\\/|  | |   __|     " << endl;
    	cout << "|  |__| |  /  _____  \\  |  |  |  | |  |____     " << endl;
    	cout << " \\______| /__/     \\__\\ |__|  |__| |_______|    " << endl;
    	cout << "                                                " << endl;
    	cout << "  ______   ____    ____  _______ .______        " << endl;
    	cout << " /  __  \\  \\   \\  /   / |   ____||   _  \\       " << endl;
    	cout << "|  |  |  |  \\   \\/   /  |  |__   |  |_)  |      " << endl;
    	cout << "|  |  |  |   \\      /   |   __|  |      /       " << endl;
    	cout << "|  `--'  |    \\    /    |  |____ |  |\\  \\----.  " << endl;
    	cout << " \\______/      \\__/     |_______|| _| `._____|  " << endl;
    	cout << "                                                " << endl;
    	cout << "                                                " << endl;
    	cout << "          your score is  "<< score << endl;
    	cout << "                                  " << endl;
    	cout << "                                  " << endl;
    	cout << "   press spacebar to continue     " << endl;
    	cout << "                                  " << endl;
    score=0;
    	while(!_kbhit() || _getch()!=' ' ){
    	}
    }
    
    }
    
    void redraw()
    {
    	system("cls");
    	for(int i=0; i<y; i++)
    	{
    		for(int j=0; j<x; j++)
    		{
    			cout << display[j][i];
    		}
    		cout << endl;
    	}
    	cout << score;
    
     
  2. macrumors 6502

    #2
    You need to replace all the calls to functions in Windows.h and remove/replace anything using the System() function.
     
  3. macrumors member

    #3
    so how exactly do i do that? are there certain mac functions to replace the windows ones/ where can i get them? also what do you mean about the System() function

    sorry i dont know a whole ton about this stuff
     
  4. macrumors 603

    #4
    Actually, it's the system() function, because C (and C++, and Objective-C) is case-sensitive.

    The system() function runs another command, as if typed into a command-line. None of the commands in your Windows code exist on Mac OS X. So you can't run them. For example, find the word system in the posted code, and make a list of the commands being executed. They're things like "cls" and "color 06". Those don't exist except on Windows.


    Then you should probably spend some time studying it, so you know what it means.
     
  5. Moderator emeritus

    kainjow

    #5
    You can use the ncurses/curses library to do this.

    Here's one way to do the main screen:

    Code:
    [color=#007400]// gcc -lcurses airstrike.c -o airstrike[/color]
    
    [color=#643820]#include [/color][color=#c41a16]<ncurses.h>[/color][color=#643820]
    [/color]
    [color=#aa0d91]int[/color] main()
    {
        WINDOW *w = initscr();
        noecho();
        nodelay(w, true);
        
        [color=#aa0d91]int[/color] displayText = [color=#1c00cf]1[/color];
        [color=#aa0d91]for[/color] (;;) {
            clear();
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"    :::     ::::::::::: :::::::::   :::::::: ::::::::::: :::::::::  ::::::::::: :::    ::: ::::::::::\n"[/color]);
            printw([color=#c41a16]"  :+: :+:       :+:     :+:    :+: :+:    :+:    :+:     :+:    :+:     :+:     :+:   :+:  :+:       \n"[/color]);
            printw([color=#c41a16]" +:+   +:+      +:+     +:+    +:+ +:+           +:+     +:+    +:+     +:+     +:+  +:+   +:+       \n"[/color]);
            printw([color=#c41a16]"+#++:++#++:     +#+     +#++:++#:  +#++:++#++    +#+     +#++:++#:      +#+     +#++:++    +#++:++#  \n"[/color]);
            printw([color=#c41a16]"+#+     +#+     +#+     +#+    +#+        +#+    +#+     +#+    +#+     +#+     +#+  +#+   +#+       \n"[/color]);
            printw([color=#c41a16]"#+#     #+#     #+#     #+#    #+# #+#    #+#    #+#     #+#    #+#     #+#     #+#   #+#  #+#       \n"[/color]);
            printw([color=#c41a16]"###     ### ########### ###    ###  ########     ###     ###    ### ########### ###    ### ##########\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"                               1st player       2nd player                                           \n"[/color]);
            printw([color=#c41a16]"                               A --> left       J --> left      don't get hit by the missiles --> V  \n"[/color]);
            printw([color=#c41a16]"                               D --> right      L --> left              made by Sean Panayi          \n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            printw([color=#c41a16]"\n"[/color]);
            [color=#aa0d91]if[/color] (displayText) {
                printw([color=#c41a16]"                             Press 1 For Single Player or 2 for Multiplayer                          \n"[/color]);
            } [color=#aa0d91]else[/color] {
                printw([color=#c41a16]"\n"[/color]);
            }
            displayText = !displayText;
            
            [color=#aa0d91]int[/color] key = getch();
            [color=#aa0d91]if[/color] (key == [color=#1c00cf]'1'[/color] || key == [color=#1c00cf]'2'[/color]) {
                [color=#aa0d91]break[/color];
            }
            
            napms([color=#1c00cf]500[/color]);
        }
        
        endwin();
        
        [color=#aa0d91]return[/color] [color=#1c00cf]0[/color];
    }
     
  6. macrumors member

    #6

    so to be clear you're replacing my functions with mac ones? with the opening page that you posted i tried it and got 9 errors

    can i run visual studio express with wine or something
     
  7. Moderator emeritus

    kainjow

    #7
    No, they aren't Mac-specific functions, they're part of the curses library which is included in Mac OS X and I'm guessing most Linux systems as well. If you google around it seems to be available for Windows, but that's not necessary as you already have it working.

    You tried it on Mac OS X? How? Please be specific.

    Why would you want to do this?
     
  8. macrumors member

    #8
    in case it would let me run this stuff without having to change it
     

Share This Page