12 Replies - 11710 Views - Last Post: 04 November 2010 - 04:59 PM Rate Topic: -----

#1 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Getting ascii art in C++

Posted 04 November 2010 - 08:55 AM

Hi there, i almost finished a (not so good :P ) 2D game, and now i'm on the final toutches, and i wanna add some ASCII art (it will be in console anyways).I thought using a char array would help, but kinda got into a problem...How do i initialize a whole char array?

Btw, these are the things that i wanna use (Not made by me) (in separate arrays)

        _                
       ( )               
       |=|               
       |=|               
   /|__|_|__|\           
  (    ( )    )          
   \|\/\"/\/|/           
     |  Y  |             
     |  |  |             
     |  |  |             
    _|  |  |             
 __/ |  |  |\            
/  \ |  |  |  \          
   __|  |  |   |         
/\/  |  |  |   |\        
 <   +\ |  |\ />  \      
  >   + \  | LJ    |     
        + \|+  \  < \    
  (O)      +    |    )   
   |             \  /\   
 ( | )   (o)      \/  )  
_\\|//__( | )______)_/   
        \\|//            






*******************************************************************************
          |                   |                  |                     |       
 _________|________________.=""_;=.______________|_____________________|_______
|                   |  ,-"_,=""     `"=.|                  |                   
|___________________|__"=._o`"-._        `"=.______________|___________________
          |                `"=._o`"=._      _`"=._                     |       
 _________|_____________________:=._o "=._."_.-="'"=.__________________|_______
|                   |    __.--" , ; `"=._o." ,-"""-._ ".   |                   
|___________________|_._"  ,. .` ` `` ,  `"-._"-._   ". '__|___________________
          |           |o`"=._` , "` `; .". ,  "-._"-._; ;              |       
 _________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______
|                   | |o;    `"-.o`"=._``  '` " ,__.--o;   |                   
|___________________|_| ;     (#) `-.o `"=.`_.--"_o.-; ;___|___________________
____/______/______/___|o;._    "      `".o|o_.--"    ;o;____/______/______/____
/______/______/______/_"=._o--._        ; | ;        ; ;/______/______/______/_
____/______/______/______/__"=._o--._   ;o|o;     _._;o;____/______/______/____
/______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_
____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____
/______/______/______/______/______/______/______/______/______/______/______/_
*******************************************************************************






 _______________________________________________________
/                                                       \
|                 Ha ha, you're dead                    |
\_________              _______________________________/ 
          \_         __/    ___---------__               
            \      _/      /              \_             
             \    /       /                 \            
              |  /       | _    _ \          \           
              | |       / / \  / \ |          \          
              | |       ||   ||   ||           |         
              | |       | \_//\\_/ |           |         
              | |       |_| (||)   |_______|   |         
              | |         |  ||     | _  / /   |         
               \ \        |_________|| \/ /   /          
                \ \_       |_|_|_|_|/|  _/___/           
                 \__>       _ _/_ _ /  |                 
                          .|_|_|_|_|   |                 
                          |           /                  
                          |__________/                   



So can anywone, please, help me? :)

Is This A Good Question/Topic? 0
  • +

Replies To: Getting ascii art in C++

#2 C++NeWb  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 03-November 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:00 AM

I used a void function and called it into main() for mine. just a suggestion though, someone who has been doing this way longer might have a better suggestion.
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:01 AM

Well, depending on the language you are using (C or C++) I would recommend you use either a char* which is a pointer to an array of characters, or a string (the C++ alternative).

Then you will have to put all the text onto a single line and separate where it is supposed to have a newline break with the newline character \n.

Hope that helps.
Was This Post Helpful? 1
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1649
  • View blog
  • Posts: 3,124
  • Joined: 30-May 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:04 AM

Well the simple thing to start with is just store them in a text file, and read them in when appropriate.

The slightly more advanced way would be to then take that text file, and output something like this
const char *some_generated_name[] = {
    "The first line of the text file",
    "The second line",
    "And so on....",
    NULL // end of the picture.
}


This is quite easily done with a few lines of say perl, or you could even make a small C program to do the same thing.
Was This Post Helpful? 0
  • +
  • -

#5 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:09 AM

^ i don't want any .txt files, just the .exe , and yes, i was thinking of going through all the characters, putting " and , and stuff, then just copy paste to initialize the array, but i did not know how to represent the ' character :)
^ ^ thanks a lot, lol, this is simple :) (i guess :P )i will use your method this time, and in the future :P (i'm using C++, so it's clean and easy :P )

PS: BetaWar, it doesn't work :)) i have lots of / characters in the picture, and it's viewed like an escape sequence...
PPS: Salem_C i get the same errors on your method too... :(
PPPS: The \\ thing does not work, i have \\ characters that will be converted to / and i don't want it...can't i disable them, or something like that? :) or can anywone give me a sword and a skull in ASCII art? :) (that wil actualy work :) )

This post has been edited by Patrunjel: 04 November 2010 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:43 AM

Sorry for double-posting, i could not edit the previous one...so i made my own ASCII thingy:

====================
|Welcome, |
|great hero, |
|what is your name?|
--------------------

but i still get : error: expected unqualified-id before string constant...please, some help? :)

Solved that too...but now i get a error at runtime, it says that NULL is not valid...
string welcome[6]={
        "====================",
        "|Welcome,          |",
        "|great hero,       |",
        "|what is your name?|",
        "--------------------",
        NULL};



Can anywone please help me? :)

This post has been edited by Patrunjel: 04 November 2010 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:49 AM

gona need to see code, also just a suggestion, things like this are difficult to paste into code, a better solution is to load it from a file into a string and output the string.
Was This Post Helpful? 0
  • +
  • -

#8 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 09:54 AM

void begin_screen(){
    string welcome[6]={
        "====================",
        "|Welcome,          |",
        "|great hero,       |",
        "|what is your name?|",
        "--------------------",
        NULL};
    int i;
    for(i=0;i<6;i++)
        cout<<welcome[i]<<endl;
}



Getting the ascii art in a separate file is out of the question, i want to just have the source code, compile, and run the game, so it's verry important to include the ascii art to the source :)
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Getting ascii art in C++

Posted 04 November 2010 - 11:59 AM

Something like this should work:

#include <stdlib.h>
#include <stdio.h>

typedef struct AsciiImageStruct {
	char **data;
	int rows;
} AsciiImage;


AsciiImage getSwordImage() {
	static char *lines[] = {
		"        _ ",
		"       ( )",
		"       |=|",
		"       |=|",
		"   /|__|_|__|\\ ",
		"  (    ( )    )",
		"   \\|\\/\\\"/\\/|/",
		"     |  Y  |",
		"     |  |  |",             
		"     |  |  |",             
		"    _|  |  | ",           
		" __/ |  |  |\\ ",         
		"/  \\ |  |  |  \\ ",       
		"   __|  |  |   |  ",     
		"/\\/  |  |  |   |\\  ",   
		" <   +\\ |  |\\ />  \\  ", 
		"  >   + \\  | LJ    | ",
		"       + \\|+  \\  < \\ ",
		"  (O)      +    |    ) ",
		"   |             \\  /\\ ",
		" ( | )   (o)      \\/  )",
		"_\\\\|//__( | )______)_/ ",
		"        \\\\|// ",
		};
	return (AsciiImage){ lines, sizeof(lines)/sizeof(char*) };
}

void printAsciiImage(AsciiImage img) {
	int i;
	for(i=0; i<img.rows; i++) {
		printf("%s\n", img.data[i]);
	}
	printf("\n");
}


int main() {
	printAsciiImage(getSwordImage());
	return 0;
}



The real trick is escaping the backslashes. Mightn't be the best way, but it was fun.

Edit: Just saw C++. Use a vector of string instead.

This post has been edited by baavgai: 04 November 2010 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#10 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1649
  • View blog
  • Posts: 3,124
  • Joined: 30-May 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 01:46 PM

> Getting the ascii art in a separate file is out of the question, i want to just have the source code,
So?

Just because your picture starts off in picture.txt, it is NOT an impediment to you compiling it in the source code.

You take picture.txt, and pass it through some kind of filter, something like
makeAnArrayFromATextFile picture.txt > picture.cpp


You then compile picture.cpp along with all your other files, and then the artwork is compiled into the program.

This post has been edited by Salem_c: 04 November 2010 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Getting ascii art in C++

Posted 04 November 2010 - 01:51 PM

I was thinking that too. Might write that. Sounds fun.
Was This Post Helpful? 0
  • +
  • -

#12 Djabby  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • Posts: 131
  • Joined: 02-November 10

Re: Getting ascii art in C++

Posted 04 November 2010 - 02:44 PM

Done :P

Usage:
Put your ASCII picture into test.txt
and add a first line which will contain the name
of your picture (that is later the function name).
Your test.txt might look like this:

TestPicture
|                  |
|                  |
|                  |
|   Hello          |
|                  |
|                  |
|                  |



Now execute the program:
./PictureToCode < test.txt > output.cpp

And admire the result:

#include <string>
#include <vector>
using namespace std;
vector<string>& TestBild()
{
	static string myPicture[] = {
		"|                  |",
		"|                  |",
		"|                  |",
		"|   Hallo          |",
		"|                  |",
		"|                  |",
		"|                  |"
	};
	static vector<string> myVector(myPicture, myPicture+sizeof(myPicture)/sizeof(string));
	return myVector;
}



Here is the code:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char** argv)
{
    string title, row;
    // First line: The title of the picture
    getline(cin, title);
    vector<string> picture;
    while(getline(cin, row)) picture.push_back(row);
    vector<string>::iterator i = picture.begin();
    cout << "#include <string>" << endl;
    cout << "#include <vector>" << endl;
    cout << "using namespace std;" << endl;
    cout << "vector<string>& " << title << "()" << endl;
    cout << "{" << endl;
    cout <<     "\tstatic string myPicture[] = {" << endl;
    while(i!=picture.end())
    {
        cout << "\t\t\"" << *i << '\"';
        ++i;
        if(i!=picture.end()) cout << ',';
        cout << endl;
    }
    cout <<     "\t};" << endl;
    cout <<     "\tstatic vector<string> myVector(myPicture, myPicture+sizeof(myPicture)/sizeof(string));" << endl;
    cout <<     "\treturn myVector;" << endl;
    cout << "}" << endl;
    cout << endl;
    return 0;
}


This post has been edited by Djabby: 04 November 2010 - 02:45 PM

Was This Post Helpful? 1
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: Getting ascii art in C++

Posted 04 November 2010 - 04:59 PM

Well, beat me. :P

Part of the point of doing a program is to escape the line properly. Also, processing more than one...

Here's my go.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void printDataLine(const string &s, ostream &out) {
	size_t endpos = s.find_last_not_of(" \t"); 
	for(size_t i=0; i<=endpos; i++) {
		char ch = s[i];
		if (ch=='\\'||ch=='"') { out << '\\'; }
		out << ch;
	}
}

void processPicture(const string &name, vector<string> pic, ostream &out) {
	out << "AsciiImage get_" << name << "() {" << endl;
	out << "\tstatic std::string lines[] = {" << endl;
	for(vector<string>::const_iterator it = pic.begin(); it!=pic.end(); ++it) {
		out << "\t\t\"";
		printDataLine(*it, out);
		out << "\"," << endl;
	}
	out << "\t};" << endl;
	out << "\tstatic AsciiImage data = { \"" << name << "\", " << pic.size() << ", lines };" << endl;
	out << "\treturn data;" << endl;
	out << "}" << endl << endl;
}

vector<string> getPicture(istream &is) {
	string line;
	vector<string> list;
	while(getline(is, line)) { 
		list.push_back(line);
	}
	return list;
}

vector<string> getPicture(const char *filename) {
	ifstream is(filename);
	vector<string> pic = getPicture(is);
	is.close();
	return pic;
}

void processPicture(const string &name, const char *filename, ostream &out = cout) {
	processPicture(name, getPicture(filename), out);
}

void processList(vector<string> names, ostream &header, ostream &body) {
	header << "#ifndef AIMG_H" << endl << "#define AIMG_H" << endl << endl;
	header << "#include <iostream>" << endl;
	header << endl;
	header << "struct AsciiImage {" << endl;
	header << "\tstd::string name;" << endl;
	header << "\tint rows;" << endl;
	header << "\tstd::string *lines;" << endl;
	header << "};" << endl;
	header << endl;
	header << "std::ostream &operator<<(std::ostream &, const AsciiImage &);" << endl;
	header << endl;

	body << "#include <iostream>" << endl;
	body << "#include \"aimg.h\"" << endl;
	body << endl;
	body << "using namespace std;" << endl;
	body << endl;
	body << "ostream &operator<<(ostream &out, const AsciiImage &img) {" << endl;
	body << "\tfor(int i=0; i<img.rows; i++) {" << endl;
	body << "\t\tout << img.lines[i] << endl;" << endl;
	body << "\t}" << endl;
	body << "\treturn out;" << endl;
	body << "}" << endl;
	body << endl;
	

	for(vector<string>::const_iterator it = names.begin(); it!=names.end(); ++it) {
		header << "AsciiImage get_" << *it << "();" << endl << endl;
		string filename(*it + ".dat");
		processPicture(*it, getPicture(filename.c_str()), body);
	}
	header << "#endif" << endl << endl;
	
}

void processListTest(vector<string> names) {
	stringstream header, body;
	processList(names, header, body);
	cout << header.str() << "-----" << endl << body.str();
}

void processList(vector<string> names) {
	ofstream header("aimg.h");
	ofstream body("aimg.cc");
	processList(names, header, body);
	header.close();
	body.close();
}


int main() {
	vector<string> names;
	names.push_back("sword");
	names.push_back("skull");
	processList(names);
	return 0;
}



I want a header file to share the with rest of the project. Code generated like this should not impact the normally written code in any way. If using a makefile, this could be included simply. Also, make the file take command line parameters.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1