4 Replies - 1987 Views - Last Post: 12 March 2009 - 01:24 PM Rate Topic: -----

#1 poptarts4liffe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 03-February 09

DrawShape

Posted 11 March 2009 - 03:55 PM

Hey,

So this is something i was working on the other day. It needs a lot of work, so I was wondering if you guys had any comments or suggestions. The more the merrier.

Thanks,
Stu


Shape.h
#include <iostream>
#include <string>
#include <windows.h>
#include "Color.h"

using namespace std;

string ScreenBuffer;

class Shape
{
private:
	int width;
	int height;
	bool intensity;
	usint color;

public:
	COORD POS;
	int jump;
	bool jumping;

	/* Constructor */
	Shape() {};
	
	Shape(int h, int w, bool i, usint c, int x, int y)
	{
		height = h;
		width = w;
		intensity = i;
		color = c;	
		POS.X = x;
		POS.Y = y;
		jump = 0;
		jumping = false;
	} 
	
	int getHeight() { return height; }
	int getWidth() { return width; }

	/* This Function draws squares and rectangles only */
	void Draw()
	{
		//For place cursor position
		HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

		// Set Shape Color
		cout << col(color, intensity);

		// To add the x/y for loop and the coord position
		COORD TEMP;
		TEMP = POS;

		for (int y = 0; y < height; y++)
		{
			TEMP.Y = POS.Y + y;
			SetConsoleCursorPosition(hConsole, TEMP);
			string line(width, ' ');
			cout << line;
			cout << endl;
		}
		//Reset Color
		cout << col(RED|BG_WHITE,false);
	}


};




Color.h
#include <iostream>
#include <windows.h>

using namespace std;


#define RED			 0x0004
#define GREEN		 0x0002
#define BLUE		 0x0001
#define WHITE		 RED|GREEN|BLUE
#define YELLOW	   RED|GREEN
#define PINK		 RED|BLUE
#define TURQUOISE	BLUE|GREEN
#define BG_RED		 0x0040
#define BG_GREEN	 0x0020
#define BG_BLUE		 0x0010
#define BG_WHITE	 BG_RED|BG_GREEN|BG_BLUE
#define BG_YELLOW	BG_RED|BG_GREEN
#define BG_PIN	   KBG_RED|BG_BLUE
#define BG_TURQUOISE BG_BLUE|BG_GREEN

typedef unsigned short int usint;
//---------------------------------------------------------------------------------	
class ostreamHelper
{
	private:
			usint n_;
			bool inten_;
			ostream& (*f_)(ostream&,usint,bool);
	public:
			ostreamHelper(ostream&(*f)(ostream&,usint, bool), usint n, bool inten):f_(f),n_(n),inten_(inten) { }
			friend ostream& operator<<(ostream& os,ostreamHelper helper)
			{
				return helper.f_(os,helper.n_,helper.inten_); }
};
//---------------------------------------------------------------------------------	
ostream& ColHelper(ostream& os, usint col, bool inten)
{
	HANDLE *hOut=new HANDLE;
	*hOut=GetStdHandle(STD_OUTPUT_HANDLE);
	if(inten==true)
		col+=0x0008;
	SetConsoleTextAttribute(*hOut,col);
	delete hOut;
	return os;
}
//--------------------------------------------------------------------------------	
ostreamHelper col(usint col, bool intensiv)
{
	return ostreamHelper(&ColHelper,col,intensiv);
}
//---------------------------------------------------------------------------------	


//EXAMPLES HOW TO USE
//int main()
//{
	/*
	cout << col(RED,true) << "hi, I'm an intensiv Red :D\n";
	cout << "I'm still red, couz u dont change it!\n";
	cout << col(BLACK|BG_WHITE,true) << "hi, I'm yellow and my BG is greeen :D\n";
	cout << col(YELLOW,false) << "now, my color is not intensiv :(\n";
	cout << col(WHITE,false) << "now I have the standard color. :> great.\n";
	system("pause");
	return 0;
	*/
//}



main.cpp
/**************************
* Draw Basic Shapes by	*
* Stuart Larsen		   *
**************************/

#include <iostream>
#include <string>
#include <stdio.h>
#include "Shape.h"

using namespace std;

//Declarations
void ClearScreen();
int PlaceCursor(const int x, const int y);
void DrawScene();

//Objects
Shape box(2, 2, true, BG_RED, 2, 18);
Shape floor(5,80,false,BG_BLUE,0,20);
Shape sun(5, 8, false,BG_YELLOW,0,0);
Shape block(3, 20, false, BG_BLUE, 20, 12);

//To keep track of Objects in Object Array
enum Objects { PLAYER, FLOOR, SUN, BLOCK };

//All the shapes. In object array so we can hit detect.
Shape shapes[10];

int main()
{
	//Assign object array values
	shapes[PLAYER] = box;
	shapes[FLOOR] = floor;
	shapes[SUN] = sun;
	shapes[BLOCK] = block;

	//Console Settings
	SetConsoleTitle("Draw Shapes");
	cout << col(RED|BG_WHITE,false);
	ClearScreen();

	//Draw Scene
	DrawScene();
	//cin.get();

	bool change = false;
	while (true) {

		//Fall down due to gravity
		if (shapes[PLAYER].POS.Y < 15) {
		  shapes[PLAYER].POS.Y++;
		  change = true;
		}

		//User Inputs
		if (GetAsyncKeyState (VK_LEFT)) {
			shapes[PLAYER].POS.X--;
			change = true;
			DrawScene();
			Sleep(100);
		}

		if (GetAsyncKeyState (VK_RIGHT)) {
			shapes[PLAYER].POS.X++;
			change = true;
			DrawScene();
			Sleep(100);
		}

		if (GetAsyncKeyState (VK_UP)) {
			box.POS.Y--;
			change = true;
			DrawScene();
			Sleep(100);
		}

		if (GetAsyncKeyState (VK_DOWN)) {
			shapes[PLAYER].POS.Y++;
			change = true;
			DrawScene();
			Sleep(100);
		}

		if (GetAsyncKeyState (VK_SPACE)) {
			change = true;
			shapes[PLAYER].jumping = true;
			shapes[PLAYER].POS.Y -= 2;
			DrawScene();
			Sleep(100);
		}

		if (!GetAsyncKeyState (VK_SPACE)) {
			shapes[PLAYER].jumping = false;
		}
		
		//Hit Detect
		if (shapes[PLAYER].POS.Y + shapes[PLAYER].getHeight() - 1 == shapes[FLOOR].POS.Y && shapes[PLAYER].POS.X + shapes[PLAYER].getWidth() > shapes[FLOOR].POS.X && shapes[PLAYER].POS.X < shapes[FLOOR].POS.X + shapes[FLOOR].getWidth()) {
			shapes[PLAYER].POS.Y = shapes[FLOOR].POS.Y - shapes[PLAYER].getHeight();
			DrawScene();
			Sleep(100);
		}

		if (shapes[PLAYER].POS.Y + shapes[PLAYER].getHeight() - 1 == shapes[BLOCK].POS.Y && shapes[PLAYER].POS.X + shapes[PLAYER].getWidth() > shapes[BLOCK].POS.X && shapes[PLAYER].POS.X < shapes[BLOCK].POS.X + shapes[BLOCK].getWidth()) {
			shapes[PLAYER].POS.Y = shapes[BLOCK].POS.Y - shapes[PLAYER].getHeight();
			DrawScene();
			Sleep(100);
		}

		//Sleep(50);
	}
}

/*****************************************************
* Right now it draws all the objects, but eventually *
* it will draw the screen buffer as a string.		*
*****************************************************/
void DrawScene()
{
	//Clear the Screen
	ClearScreen();

	//Draw Boxs
	shapes[FLOOR].Draw();
	shapes[PLAYER].Draw();
	shapes[SUN].Draw();
	shapes[BLOCK].Draw();

	//Stats
	PlaceCursor(1, 24);
	cout << shapes[PLAYER].POS.X << ", " << shapes[PLAYER].POS.Y << " " << shapes[PLAYER].jumping;// << "				 Lives: (3 little boxs) " << endl;
}

//////////////////////////////////////////////////////
// Game Engine Stuff
//////////////////////////////////////////////////////


/* Clears the Screen in an more efficent manner than system("cls"); */
void ClearScreen ( void )
{
  DWORD n;						 /* Number of characters written */
  DWORD size;					  /* number of visible characters */
  COORD coord = {0};			   /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

int PlaceCursor(const int x, const int y) {

	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD PlaceCursorHere;
	PlaceCursorHere.X = x;
	PlaceCursorHere.Y = y;

	SetConsoleCursorPosition(hConsole, PlaceCursorHere);
	return 0;
}


This post has been edited by poptarts4liffe: 11 March 2009 - 04:34 PM


Is This A Good Question/Topic? 0
  • +

Replies To: DrawShape

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: DrawShape

Posted 11 March 2009 - 04:01 PM

Didn't take a very thorough look at it, but why do you do #include "Shape.h" inside of Shape.h?
Was This Post Helpful? 0
  • +
  • -

#3 poptarts4liffe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 03-February 09

Re: DrawShape

Posted 11 March 2009 - 04:35 PM

Oops lol, I mixed them up. Thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: DrawShape

Posted 11 March 2009 - 05:07 PM

Very interesting! Glad you talked to a girl "irl," go you!

Here's an improved version of what you have (one small flaw):
EDIT: All bugs (except one "bug") have been fixed! :)

- Shape.h
#include <iostream>
#include <windows.h>
#include "Color.h"
using namespace std;

class Shape {

    private:
        int width;
        int height;
        bool intensity;
        usint color;

    public:
        COORD POS;
        bool jumping;

    /* Constructor */
    Shape() {};
    Shape(int h, int w, bool i, usint c, int x, int y);
    int getHeight();
    int getWidth();
    void Draw();
    void Erase();
    void UpdatePos();

};

Shape::Shape(int h, int w, bool i, usint c, int x, int y) {

    height = h;
    width = w;
    intensity = i;
    color = c;
    POS.X = x;
    POS.Y = y;
    jumping = false;
}

int Shape::getHeight() { return height; }
int Shape::getWidth() { return width; }

void Shape::Draw() {

	//For place cursor position
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	// Set Shape Color
	cout << col(color, intensity);

	// To add the x/y for loop and the coord position
	COORD TEMP;
	TEMP = POS;

	for (int y = 0; y < height; y++) {

		TEMP.Y = POS.Y + y;
		SetConsoleCursorPosition(hConsole, TEMP);
		string line(width, ' ');
		cout << line << endl;
	}

	//Reset Color
	cout << col(RED | BG_WHITE, false);

	return;
}

void Shape::Erase() {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD TEMP;
	TEMP = POS;

	for (int y = 0; y < height; y++) {
		TEMP.Y = POS.Y + y;
		SetConsoleCursorPosition(hConsole, TEMP);
		string line(width, ' ');
		cout << line << endl;
    }

    return;
}

void Shape::UpdatePos() {

    PlaceCursor(1, 24);
    cout << POS.X << ", " << POS.Y << " " << jumping;

    return;
}


- main.cpp
#include <iostream>
#include <TextControl.h>
#include "Shape.h"
using namespace std;

/* Objects within the game */
Shape box(2, 2, true, BG_RED, 2, 18);       /* Player */
Shape floor(5, 80, false, BG_BLUE, 0, 20);  /* The ground */
Shape sun(5, 8, false, BG_YELLOW, 0, 0);    /* The sun (upper left) */
Shape block(3, 20, false, BG_BLUE, 20, 12); /* The block in center */

/* To keep track of the objects in the "Object Array" */
enum Objects { PLAYER, FLOOR, SUN, BLOCK };

/* All the shapes in the "object array" so we can detect collisions */
Shape shapes[5];

void InitiateGame() {

    ClearConsole(BLACK, GREY);

    /* Draw the objects */
    shapes[FLOOR].Draw();
    shapes[PLAYER].Draw();
    shapes[SUN].Draw();
    shapes[BLOCK].Draw();

    /* Draw the statistics: X, Y, jumping or not */
    PlaceCursor(1, 24);
    cout << shapes[PLAYER].POS.X << ", " << shapes[PLAYER].POS.Y;

    return;
}

int main() {

    /* Assign the object array values */
    shapes[BLOCK]  = block;
    shapes[FLOOR]  = floor;
    shapes[PLAYER] = box;
    shapes[SUN]    = sun;

    /* Initiate the game (draw all the boxes, setup the background, etc */
    InitiateGame();

    /* Is gravity in effect? */
    bool Gravity = false;

    while (true) {

        /* If the user IS on the box, hold his position (and remove gravity so he doesn't bounce) */
        if (shapes[PLAYER].POS.X == shapes[BLOCK].POS.X - shapes[BLOCK].getWidth() && shapes[PLAYER].getHeight() == shapes[BLOCK].POS.Y - shapes[BLOCK].getHeight()) {
            Gravity = false;
            shapes[PLAYER].Erase();
            shapes[PLAYER].POS.Y = shapes[BLOCK].POS.Y - shapes[PLAYER].getHeight();
            shapes[PLAYER].UpdatePos();
            shapes[PLAYER].Draw();
        }

        /* Gravity pulls you down! Thank you, Sir Isaac Newton! <3 */
        if (Gravity) {
            shapes[PLAYER].Erase();
            shapes[PLAYER].POS.Y++;
            shapes[PLAYER].UpdatePos();
            shapes[PLAYER].Draw();
            shapes[BLOCK].Draw();
        }

        /* Trigger gravity - unless player is on the box in the center */
        if (shapes[PLAYER].POS.Y > 17) { Gravity = false; }
        else if (shapes[PLAYER].POS.X < (shapes[BLOCK].getWidth() + 1) - shapes[PLAYER].getWidth() || shapes[PLAYER].POS.X > shapes[BLOCK].POS.X + shapes[BLOCK].getWidth() - 1 || shapes[PLAYER].POS.Y + shapes[PLAYER].getHeight() != shapes[BLOCK].POS.Y)
        { Gravity = true; } else { Gravity = false; }

        /* Cannot pass the left side of the screen */
        if (GetAsyncKeyState(VK_LEFT) && shapes[PLAYER].POS.X > 0 + shapes[PLAYER].getWidth() - 2) {
            shapes[PLAYER].Erase();
            shapes[PLAYER].POS.X--; /* Walk left */
            shapes[PLAYER].UpdatePos();
            shapes[PLAYER].Draw();
            shapes[BLOCK].Draw();
        }

        /* Cannot pass the right side of the screen */
        if (GetAsyncKeyState(VK_RIGHT) && shapes[PLAYER].POS.X < 80 - shapes[PLAYER].getWidth()) {
            shapes[PLAYER].Erase();
            shapes[PLAYER].POS.X++; /* Walk right */
            shapes[PLAYER].UpdatePos();
            shapes[PLAYER].Draw();
            shapes[BLOCK].Draw();
        }

        /* Cannot pass the floor */
        if (GetAsyncKeyState(VK_DOWN) && shapes[PLAYER].POS.Y < 18 && !Gravity) {
            shapes[PLAYER].Erase();
            shapes[PLAYER].POS.Y++; /* Sink down by force */
            shapes[PLAYER].UpdatePos();
            shapes[PLAYER].Draw();
            shapes[BLOCK].Draw();
        }

        /* Cannot pass the ceiling */
        if (GetAsyncKeyState(VK_SPACE) && shapes[PLAYER].POS.Y > 0) {

            /* You cannot pass the bottom of the box */
            if (shapes[PLAYER].POS.X + shapes[PLAYER].getWidth() > shapes[BLOCK].POS.X && shapes[PLAYER].POS.X < shapes[BLOCK].POS.X + shapes[BLOCK].getWidth() && shapes[PLAYER].POS.Y == shapes[BLOCK].POS.Y + shapes[BLOCK].getHeight()) {
                Gravity = false;
                shapes[PLAYER].Erase();
                shapes[PLAYER].POS.Y = shapes[BLOCK].POS.Y + shapes[BLOCK].getHeight();
                shapes[PLAYER].UpdatePos();
                shapes[PLAYER].Draw();
            } else {
                shapes[PLAYER].Erase();
                shapes[PLAYER].POS.Y -= 2; /* Float up */
                shapes[PLAYER].UpdatePos();
                shapes[PLAYER].Draw();
                shapes[BLOCK].Draw();
            }
        }

        Sleep(50);
    }
    
    return 0;
}


Welcome, hope that was helpful... Feel free to use the "Thank you" button. :P
FYI: You need to fix two things:
- When you stand on the box (in air): You (somehow) grow a block taller
- You can fall through the floor


Now everybody can have fun fiddling around. :)

This post has been edited by Hyper: 12 March 2009 - 01:20 PM

Was This Post Helpful? 1
  • +
  • -

#5 Hyper   User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: DrawShape

Posted 12 March 2009 - 01:24 PM

Oh! Before I forget, there's one thing you (anybody who wants to add onto or improve this some more) can do:
Instead of redrawing the box every time! You could have it:
- Draw the "box" (player) whenever he moves
- Draw the "box" (floating object) whenever the player goes ontop it
- Draw the "sun" (floating in the upper left) whenever the player goes ontop it
- Create a subroutine to check if the player is ontop of any other objects.
also...
- Create a "velocity" so the "gravity" is more realistic

Hope somebody has fun!

EDIT: Forgot to include the relevant code from TextControl, thanks poptarts4liffe :)
enum {
    BLACK       = 0,
    DARK_BLUE   = 1,
    DARK_GREEN  = 2,
    TEAL        = 3,
    DARK_RED    = 4,
    DARK_PURPLE = 5,
    GOLD        = 6,
    GREY        = 7,
    DARK_WHITE  = 8,
    BLUE        = 9,
    GREEN       = 10,
    CYAN        = 11,
    RED         = 12,
    PURPLE      = 13,
    YELLOW      = 14,
    WHITE       = 15
};

void SetColor(const int foreground, const int background) {

    int Color = foreground + (background * 16);
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hConsole, Color);

    return;
}

void ClearConsole(const int foreground, const int background) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD coordScreen = { 0, 0 };
    DWORD cCharsWritten;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD dwConSize;

    if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

    SetColor(foreground, background);
    if (!FillConsoleOutputCharacter(hConsole, (TCHAR) ' ', dwConSize, coordScreen, &cCharsWritten)) { return; }
    if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) { return; }
    if (!FillConsoleOutputAttribute( hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten)) { return; }

    return;
}

void PlaceCursor(const int x, const int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD PlaceCursorHere;
    PlaceCursorHere.X = x;
    PlaceCursorHere.Y = y;

    SetConsoleCursorPosition(hConsole, PlaceCursorHere);
    return;
}


There! :D

This post has been edited by Hyper: 12 March 2009 - 04:56 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1