Oh my glorious code!

Started by
20 comments, last by cr88192 10 years, 8 months ago

When I was 12 I only knew how to put discs on my Playstation biggrin.png Later I learnt to put discs on my PC and the rest is history tongue.png

Did you had automatic formatting or you wrote it just like that?

Yes, I had automatic formatting enabled.

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh
Advertisement

Ahhh, at 12 I was hacking the Half-Life SDK. Added ricochets (if the reflection angle was sufficiently shallow) and bullets that could go through walls. And made the UI green tongue.png

My first encounter with the horrors that are/were Visual C++ 6 and systems hungarian notation.


I don't have any code from when I was that age, as around the age of 14 or so, an HDD crash had wiped out everything I had before this point

Same, only my HDD was random girly-pop cassette tapes I "found" somewhere in the vicinity of my sister's tape player...

system("cls")...oh how those days were glorious!


10 CLS
20 "do stuff"
30 GOTO 20

I cannot remember the books I've read any more than the meals I have eaten; even so, they have made me.

~ Ralph Waldo Emerson

If stdout supports ANSI escape codes (e.g. the terminals on *nix or DOS with ANSI.SYS) you could use the escape codes for even more interesting stuff without moving away from the standard library. Colors everywhere!

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

If stdout supports ANSI escape codes (e.g. the terminals on *nix or DOS with ANSI.SYS) you could use the escape codes for even more interesting stuff without moving away from the standard library. Colors everywhere!

I've been having so much fun with those lately, they are really easy to use as well. Shame they aren't 100% portable, though. I know there's ncurses (and its Windows counterpart PDCurses) but sometimes you just want colors here and there without engineering a complete user interface solution.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

If stdout supports ANSI escape codes (e.g. the terminals on *nix or DOS with ANSI.SYS) you could use the escape codes for even more interesting stuff without moving away from the standard library. Colors everywhere!

I've been having so much fun with those lately, they are really easy to use as well. Shame they aren't 100% portable, though. I know there's ncurses (and its Windows counterpart PDCurses) but sometimes you just want colors here and there without engineering a complete user interface solution.

Dude, ncurses is as easy as or even easier than standart cout in C++. You don't need to engineer complete UI interface or nothing.

Some guy on youtube was interested in this game, and said I should rewrite it in ncurses, so I began working on it.

Look my simple graphics class, which works very well, you can use it too, if you want to.


// GraphicsSystem.h


#ifndef GRAPHICSSYSTEM_H
#define GRAPHICSSYSTEM_H


class GraphicsSystem
{
private:
    static const int SCREEN_WIDTH = 79;
    static const int SCREEN_HEIGHT = 24;
    char m_frameBuffer[SCREEN_WIDTH][SCREEN_HEIGHT];

    void renderBuffer();
public:
    GraphicsSystem();
    ~GraphicsSystem();

    void clearFramebuffer();
    void renderPixel(int x, int y, char ch);
    void render();
    int getScreenWidth();
    int getScreenHeight();
};

#endif // GRAPHICSSYSTEM_H


//--------------------------------------------------------------
//GraphicsSystem.cpp


#include <curses.h>
#include "GraphicsSystem.h"

GraphicsSystem::GraphicsSystem()
{
	clearFramebuffer();
	initscr();
	cbreak();
	timeout(50);
	keypad(stdscr, TRUE);
}

GraphicsSystem::~GraphicsSystem()
{
    endwin();
}

void GraphicsSystem::render()
{
    wclear(stdscr);
    renderBuffer();
    clearFramebuffer();
    wrefresh(stdscr);
}

int GraphicsSystem::getScreenWidth(){
    return SCREEN_WIDTH;
}

int GraphicsSystem::getScreenHeight(){
    return SCREEN_HEIGHT;
}

void GraphicsSystem::renderPixel(int x, int y, char ch)
{
    m_frameBuffer[x][y] = ch;
}

void GraphicsSystem::renderBuffer()
{
    for(int y = 0; y < SCREEN_HEIGHT; y++)
        for(int x = 0; x < SCREEN_WIDTH; x++)
        {
            mvwaddch(stdscr, y, x, m_frameBuffer[x][y]);
        }
}

void GraphicsSystem::clearFramebuffer()
{
    for(int y = 0; y < SCREEN_HEIGHT; y++)
        for(int x = 0; x < SCREEN_WIDTH; x++)
        {
            m_frameBuffer[x][y] = ' ';
        }
}



And you can draw stuff with ease just like this:


#include <curses.h>
#include <cstdlib>
#include <cmath>
#include "GraphicsSystem.h"

GraphicsSystem graphicsSystem;

int main()
{
    while(true){
        for(int x = 0; x < graphicsSystem.getScreenWidth(); x++)
        {
            for(int y = 0; y < graphicsSystem.getScreenHeight(); y++)
            {
                graphicsSystem.renderPixel(x,y,'x'); // render char 'x', or "pixel" on the screen
            }
        }
        graphicsSystem.render(); //clears screen, and displays all chars
    }
	return 0;
}

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

I think he meant he just wanted to add colors to the terminal as-is and nothing else. Honestly though, if one starts adding things like that one may as well end up just taking over the entire console, which is what ncurses does, pretty much.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

I think he meant he just wanted to add colors to the terminal as-is and nothing else. Honestly though, if one starts adding things like that one may as well end up just taking over the entire console, which is what ncurses does, pretty much.

Not necessarily. What I meant was doing some minimal coloring of whatever you're outputting, without taking over the entire console, where simply wrapping up a string constant in an ANSI escape sequence is much easier than working with ncurses when the extra flexibility (menu helpers, borders, and so on) are not needed. Different solutions to different problems, really. ncurses is a nifty little library, and is great for making roguelikes and other games which lend themselves well to a 2D ASCII representation, as well as creating complete console interfaces, but that doesn't mean you need to use it every time you need to push a few colors to a terminal... sometimes a script is just a script wink.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement