Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C++

Name: Anonymous 2011-02-15 19:21

Professional enterprise C++ programmer here

all you faggots talk about LISP but can you produce any enterprise-grade code?




//The headers

#include "SDL/SDL.h"

#include "SDL/SDL_image.h"

#include <string>



//The screen attributes

const int SCREEN_WIDTH = 640;

const int SCREEN_HEIGHT = 480;

const int SCREEN_BPP = 32;



//The frame rate

const int FRAMES_PER_SECOND = 20;



//The dimensions of the dot

const int DOT_WIDTH = 20;

const int DOT_HEIGHT = 20;



//The surfaces

SDL_Surface *dot = NULL;

SDL_Surface *screen = NULL;



//The event structure

SDL_Event event;



//The dot that will move around on the screen

class Dot

{

    private:

    //The X and Y offsets of the dot

    int x, y;



    //The velocity of the dot

    int xVel, yVel;



    public:

    //Initializes the variables

    Dot();



    //Takes key presses and adjusts the dot's velocity

    void handle_input();



    //Moves the dot

    void move();



    //Shows the dot on the screen

    void show();

};



//The timer

class Timer

{

    private:

    //The clock time when the timer started

    int startTicks;



    //The ticks stored when the timer was paused

    int pausedTicks;



    //The timer status

    bool paused;

    bool started;



    public:

    //Initializes variables

    Timer();



    //The various clock actions

    void start();

    void stop();

    void pause();

    void unpause();



    //Gets the timer's time

    int get_ticks();



    //Checks the status of the timer

    bool is_started();

    bool is_paused();

};



SDL_Surface *load_image( std::string filename )

{

    //The image that's loaded

    SDL_Surface* loadedImage = NULL;



    //The optimized surface that will be used

    SDL_Surface* optimizedImage = NULL;



    //Load the image

    loadedImage = IMG_Load( filename.c_str() );



    //If the image loaded

    if( loadedImage != NULL )

    {

        //Create an optimized surface

        optimizedImage = SDL_DisplayFormat( loadedImage );



        //Free the old surface

        SDL_FreeSurface( loadedImage );



        //If the surface was optimized

        if( optimizedImage != NULL )

        {

            //Color key surface

            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );

        }

    }



    //Return the optimized surface

    return optimizedImage;

}



void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )

{

    //Holds offsets

    SDL_Rect offset;



    //Get offsets

    offset.x = x;

    offset.y = y;



    //Blit

    SDL_BlitSurface( source, clip, destination, &offset );

}



bool init()

{

    //Initialize all SDL subsystems

    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )

    {

        return false;

    }



    //Set up the screen

    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );



    //If there was an error in setting up the screen

    if( screen == NULL )

    {

        return false;

    }



    //Set the window caption

    SDL_WM_SetCaption( "Move the Dot", NULL );



    //If everything initialized fine

    return true;

}



bool load_files()

{

    //Load the dot image

    dot = load_image( "dot.bmp" );



    //If there was a problem in loading the dot

    if( dot == NULL )

    {

        return false;

    }



    //If everything loaded fine

    return true;

}



void clean_up()

{

    //Free the surface

    SDL_FreeSurface( dot );



    //Quit SDL

    SDL_Quit();

}



Dot::Dot()

{

    //Initialize the offsets

    x = 0;

    y = 0;



    //Initialize the velocity

    xVel = 0;

    yVel = 0;

}



void Dot::handle_input()

{

    //If a key was pressed

    if( event.type == SDL_KEYDOWN )

    {

        //Adjust the velocity

        switch( event.key.keysym.sym )

        {

            case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;

            case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;

            case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;

            case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;

        }

    }

    //If a key was released

    else if( event.type == SDL_KEYUP )

    {

        //Adjust the velocity

        switch( event.key.keysym.sym )

        {

            case SDLK_UP: yVel += DOT_HEIGHT / 2; break;

            case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;

            case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;

            case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;

        }

    }

}



void Dot::move()

{

    //Move the dot left or right

    x += xVel;



    //If the dot went too far to the left or right

    if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )

    {

        //move back

        x -= xVel;

    }



    //Move the dot up or down

    y += yVel;



    //If the dot went too far up or down

    if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )

    {

        //move back

        y -= yVel;

    }

}



void Dot::show()

{

    //Show the dot

    apply_surface( x, y, dot, screen );

}



Timer::Timer()

{

    //Initialize the variables

    startTicks = 0;

    pausedTicks = 0;

    paused = false;

    started = false;

}



void Timer::start()

{

    //Start the timer

    started = true;



    //Unpause the timer

    paused = false;



    //Get the current clock time

    startTicks = SDL_GetTicks();

}



void Timer::stop()

{

    //Stop the timer

    started = false;



    //Unpause the timer

    paused = false;

}



void Timer::pause()

{

    //If the timer is running and isn't already paused

    if( ( started == true ) && ( paused == false ) )

    {

        //Pause the timer

        paused = true;



        //Calculate the paused ticks

        pausedTicks = SDL_GetTicks() - startTicks;

    }

}



void Timer::unpause()

{

    //If the timer is paused

    if( paused == true )

    {

        //Unpause the timer

        paused = false;



        //Reset the starting ticks

        startTicks = SDL_GetTicks() - pausedTicks;



        //Reset the paused ticks

        pausedTicks = 0;

    }

}



int Timer::get_ticks()

{

    //If the timer is running

    if( started == true )

    {

        //If the timer is paused

        if( paused == true )

        {

            //Return the number of ticks when the timer was paused

            return pausedTicks;

        }

        else

        {

            //Return the current time minus the start time

            return SDL_GetTicks() - startTicks;

        }

    }



    //If the timer isn't running

    return 0;

}



bool Timer::is_started()

{

    return started;

}



bool Timer::is_paused()

{

    return paused;

}



int main( int argc, char* args[] )

{

    //Quit flag

    bool quit = false;



    //The dot that will be used

    Dot myDot;



    //The frame rate regulator

    Timer fps;



    //Initialize

    if( init() == false )

    {

        return 1;

    }



    //Load the files

    if( load_files() == false )

    {

        return 1;

    }



    //While the user hasn't quit

    while( quit == false )

    {

        //Start the frame timer

        fps.start();



        //While there's events to handle

        while( SDL_PollEvent( &event ) )

        {

            //Handle events for the dot

            myDot.handle_input();



            //If the user has Xed out the window

            if( event.type == SDL_QUIT )

            {

                //Quit the program

                quit = true;

            }

        }



        //Move the dot

        myDot.move();



        //Fill the screen white

        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );



        //Show the dot on the screen

        myDot.show();



        //Update the screen

        if( SDL_Flip( screen ) == -1 )

        {

            return 1;

        }



        //Cap the frame rate

        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )

        {

            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );

        }

    }



    //Clean up

    clean_up();



    return 0;

}


this is what a professionals code looks like

remember it.

Name: Anonymous 2011-02-15 19:30

Got it.  I will be sure to maintain a whitespace-to-code ratio of at least 10x in my future ENTERPRISE projects.

Name: Anonymous 2011-02-15 19:34

my code looks just as professional and i started C two weeks ago.

also, your code does nothing and Frozen Void could probably write it in 20 lines of haskell.

Name: Anonymous 2011-02-15 19:40

>>3
FrozenVoid only knows Javascript.

Name: Anonymous 2011-02-15 21:34

sup lazy foo

Name: Anonymous 2011-02-15 21:46

>SDL

sure is 2003 in here

Name: Anonymous 2011-02-15 21:56

sure is /g/ in here

Name: Anonymous 2011-02-15 22:40

Why do you have so many blank lines? A few are okay every now and again, but this makes your code hard to read.

Name: Anonymous 2011-02-15 23:46

>>7
fuck you fag

Name: haxus_the_anus_poster 2011-02-15 23:51

You should use more comments and whitespace.

Name: Anonymous 2011-02-15 23:57

>>1
pass-by-value instead of pass-by-constref
retarded blank lines
retarded spaces: derp ( derp ) ... seriously?
instance class members declared outside of classbody, when class isn't even used as library
SDL

Well, I'd say OP is a VisualBASIC programmer, and probably copypasta'd the code from some website.
No ``learned'' C++ Programmer would actually write code like that. Only People who clearly never learned C++ would.

Name: Anonymous 2011-02-16 0:29

>>11
fuck you faggot
learn to quote
and go back to /g/ before insulting my code

Name: Anonymous 2011-02-16 0:57

>>12
You seem upset.

Name: Anonymous 2011-02-16 1:00

>>13
Sometimes, I can be.

Name: Anonymous 2011-02-16 1:02

>>14
``u mad''

Prease to go back to /g/

Name: Anonymous 2011-02-16 1:07

>>15
``u mad''
Prease to go back to /g/
[/o]
IHBT

Name: Anonymous 2011-02-16 1:26

>>1-16

Back to the imageboards.

Name: Anonymous 2011-02-16 1:38

>>17
Agree with this /prog/dolyte.

Name: Anonymous 2011-02-16 1:52

>>17
no u

Name: Anonymous 2011-02-16 5:27

>>19
Polecat Kebabs.

Name: Fuck off, !Ep8pui8Vw2 2011-02-16 6:10

>>20
Fuck off, ``faggot''.

Name: Anonymous 2011-02-16 6:12

>>17

fuck you faggot

Name: Anonymous 2011-02-16 6:38

>>4
proof?

Name: Anonymous 2011-02-16 7:21

HAX MY AUTISM

Name: Anonymous 2011-02-16 16:00

if this was pro you would have made a comment header that tells the person looking at the code what to do

Name: Anonymous 2011-02-16 17:21

Whats the problem with not telling the user what went wrong when something goes wrong?

>run game.exe
>game.exe crashes silently

User tries to find problem:
all files there, other games seem to work, too. correct installed and the drivers are the newest, too.

User asks on a support forum:
>"LOLOL buy new video card ^____^"
>"install newest driverz, moron"

User doesnt buy any more games from the company.

"Enterprise" programmer hopefully gets fired.

btw: actual problem: user has old system which doesnt support 32 bit color depth.

this code is shit in so many ways, not even mentioning how unreadable it is.

Also, why would anyone write such code in sepples and not plain C? This is something i just didnt understand for years...
Also, IHBT

Name: Anonymous 2011-02-16 18:03

Here's my /prog/ standard library replacement for std::deque, with an emphasis on performance. Insertion and erasure of elements is between 5-10x faster than the libstdc++, PJ Plauger (MSVC++), and STLPort implementations, depending on the precise function being invoked. It also has much better memory allocation characteristics, able to recycle unused blocks from either end of the data structure. Furthermore, it actually has a lower code footprint than the other implementations, compiling down to leaner code.

Unfortunately, this isn't explicitly useable because I am not including the header dependencies. The project name also isn't "prog", I've just changed it to that so as to help keep my identity anonymous. Exception safety isn't currently a concern, because I use this on embedded systems where I keep exception handling turned off. Shouldn't be too hard to add in exception safe versions using #ifdef/#else/#endif conditional compilation.

It has support for C++0x r-value references and move semantics, with backwards compatible move support for C++98.

Public member functions in the class interfaces are spaced out because I eventually plan to document it with doxygen.

I wrote it in an afternoon, then wrote the unit tests and benchmark code the following day.

I am not posting the code here directly, as I would have to break it up too much.

Part 1 - http://codepad.org/q0321gNJ
Part 2 - http://codepad.org/yBMbnyPQ

Name: Anonymous 2011-02-16 20:22

>>27
We should write a /prog/STL.

Don't change these.
Name: Email:
Entire Thread Thread List