Page 1 of 1

Beginner C++ (noob alert)

Posted: May 4th, 2020, 11:48 pm
by diniz
so i started the beginner c++ series and i was trying to create a function to drawn a line of pixels, i am using the chili framework btw

so i declared the function like that
void ImageLine(int n, int x, int y, int c1, int c2, int c3);
and defined it like that

Code: Select all

void Game::ImageLine(int n, int x, int y, int c1, int c2, int c3)
                                 (pixels) (position)   (         colors         )
{
    for (nline == 0; nline < n; nline ++) {
        gfx.PutPixel(x + nline, y, c1, c2, c3);
    }

}
(i have nline declared in Game.h)

so i assumed that when i called the function it would print a horizontal line wich would be "n" pixels in size but i dont get a single one so it looks like
if i want to have a specific shape i can only have a function to it if i have a PutPixel to every single one of the pixels?
or is my code just bad and as ai am a begginer maybe what im trying to show cant be understanded so if thats the case sorry '-'

Re: Beginner C++ (noob alert)

Posted: May 5th, 2020, 2:06 am
by albinopapa
From the looks of it, this code should work just fine assuming that nline and y are valid numbers between 0 and Graphics::ScreenHeight and n and x are valid numbers between 0 and Graphics::ScreenWidth.

Here's how I would code it just for comparison:
Since it's a graphics function, I'd declare it in Graphics.h

Code: Select all

// Graphics.h

class Graphics
{
    // framework code hidden
    // ...
    // 
    
    // 
    void DrawHorizontalLine( int x_, int y_, int length_, Color color_ );
};
Then define in Graphics.cpp

Code: Select all

// Graphics.cpp
void Graphics::DrawHorizontalLine( int x_, int y_, int length_, Color color_ )
{
    for( int x = x_; x < x_ + length_; ++x )
    {
        PutPixel( x, y_, color_ );
    }
}
The call it from Game::ComposeFrame()

Code: Select all

                        //( x,   y, length,         Color )
    gfx.DrawHorizontalLine( 0, 300,    300, Colors::Green );
For any generic draw functions, I'd place them in the Graphics class. For specific draw functions, I'd keep them with the classes that have all the data available. I'd consider the DrawHorizontalLine a generic draw function.

Re: Beginner C++ (noob alert)

Posted: May 5th, 2020, 4:14 pm
by diniz
albinopapa wrote:
May 5th, 2020, 2:06 am
From the looks of it, this code should work just fine assuming that nline and y are valid numbers between 0 and Graphics::ScreenHeight and n and x are valid numbers between 0 and Graphics::ScreenWidth.

Here's how I would code it just for comparison:
Since it's a graphics function, I'd declare it in Graphics.h

Code: Select all

// Graphics.h

class Graphics
{
    // framework code hidden
    // ...
    // 
    
    // 
    void DrawHorizontalLine( int x_, int y_, int length_, Color color_ );
};
Then define in Graphics.cpp

Code: Select all

// Graphics.cpp
void Graphics::DrawHorizontalLine( int x_, int y_, int length_, Color color_ )
{
    for( int x = x_; x < x_ + length_; ++x )
    {
        PutPixel( x, y_, color_ );
    }
}
The call it from Game::ComposeFrame()

Code: Select all

                        //( x,   y, length,         Color )
    gfx.DrawHorizontalLine( 0, 300,    300, Colors::Green );
For any generic draw functions, I'd place them in the Graphics class. For specific draw functions, I'd keep them with the classes that have all the data available. I'd consider the DrawHorizontalLine a generic draw function.
that worked just fine now, but still i dont get why the code i made before wasnt working as you said yourself it should work, maybe just becouse i declared in Game.h and defined it in Game.cpp?

Re: Beginner C++ (noob alert)

Posted: May 5th, 2020, 4:47 pm
by diniz
one more thing, i then made another function to draw rectangles just by calling the DrawHorizontalLine (i named it DrawHLine) several times like that

Code: Select all

void DrawFRetangulo(int x_, int y_, int length_, int hight_, Color color_);
the only thing new its that now we need a hight to say how many times DrawHLine will be used

defined like that

Code: Select all

void Graphics::DrawFRetangulo(int x_, int y_, int length_, int hight_, Color color_)
{
	int y;
	for (y = y_; y < y_ + hight_; y ++) {
		DrawHLine(x_, y, length_, color_);
	}
}
so every thing works fine, i just wanted to know why i had to declare "y" here and not on the DrawHLine's for
for what i studied before whe you use a for you would not need to declare the variable outside of it but if i dont do it i get y is undefined

Re: Beginner C++ (noob alert)

Posted: May 5th, 2020, 6:51 pm
by albinopapa
so writing

Code: Select all

void Graphics::DrawFRetangulo(int x_, int y_, int length_, int hight_, Color color_)
{
	for (int y; = y_; y < y_ + hight_; y ++) {
		DrawHLine(x_, y, length_, color_);
	}
}
doesn't work?

As far as why your original line drawing algorithm didn't work, I'd need to have a copy of your Visual Studio solution with the original code that wasn't working to be able to answer that. If I had to wager a guess though, is something wrong with the variables you passed to the function, but I couldn't say for sure without being able to debug it line by line using a compiler. You could also do the same by setting a break point ( move blinking cursor to line with function call and press F9 on keyboard or go to the Debug menu and choose Toggle Breakpoint ), then you can check the variables' values.

Get to know your debugger well, it's going to be a huge help no matter how long you program.