Drawing directly to display

Michael Samuel wrote:

Ok, this question is quite a simple one (I hope)…

I want to make a very simple widget set for a program I’m writing (eg. buttons
and writing to go on them), and I feel rather silly drawing a BMP for each
one :wink:

I read the example in the docs of writing a single pixel, but that didn’t give
me any (workable) ideas on how to draw to the screen.

Anyway, could somebody give me some examples of how to do the following things:

  1. Draw a square, and fill in with a different colour to the outline

First off…don’t use that disgusting screenlib…avoid the yucky C++ altogether
and just do it yourself!

Second off, to set a pixel on the screen do this:

*(surface->pixels + (y * WIDTH) + x) = pixel;

Where:

x is the x location of the area you want to set the pixel.
y…(ditto)

WIDTH is the width of the screen in pixels. (You could replace that with
surface->pitch, make sure to use 8 bit display)

pixel is the color you want to set the pixel to. NOTE: Make sure to set palette!

surface is the SDL_Surface you want to put the pixel on.

NOTE: these functions are intended to run on an 8 bit display. Also, these
functions are NOT optimized. You should look for faster equivalents.

Make a function to draw a horizontal line:
We only need a x, y, and another x value. (the other y value will be the same
cause it’s horizontal!)

void
Horiz_Line (SDL_Surface * surface, int x1, int y1, int x2, char color)

{

Uint8 *pixel;

pixel = surface->pixels + (y1 * screen->pitch) + x1;

/* x1, and x2, CHECK values for validity (e.g. x1= 2, x2 MUST be more than x1!, x1
must be less than x2! (or equal) */

while (x1 <= x2)
{

  *pixel = color;
  ++pixel;
  ++x1;

}

}

Now make a function to draw a vertical line, only this time add the width of the
surface instead of 1 to the pointer.
We only need a x, y, and second y value here.

void
Verti_Line (SDL_Surface *surface, int x1, int y1, int y2, char color)

{

Uint8 *pixel;

pixel = surface->pixels + (y1 * screen->pitch) + x1;

/* make sure to check validity of y1, and y2 */

while (y1 <= y2)
{

  *pixel = color;
  pixel += screen->pitch;
  ++y1;

}

}

To draw a filled box do this:
void

Fill_Box (SDL_Surface * surface, int x1, int y1, int x2, int y2, char color)
{

while (y1 <= y2)
{

/* You could use the code in Horiz_Line here if you want a faster line without the
function call overhead /
/
memset would work here too as a replacement for horizline, would memset be
faster? /
/
You might also consider some error checking to make sure y1 is less than or
equal to y2.

  Horiz_Line (surface, x1, y1, x2, color);
  y1++;
}

}

Now. If you want to draw a outlined box do this:

void
Empty_Box (SDL_Surface *surface, int x1, int y1, int x2, int y2, char color)
{
Horiz_Line (surface, x1, y1, x2, color);
Horiz_Line (surface, x1, y2, x2, color);
Verti_Line (surface, x1, y1, y2, color);
Verti_Line (surface, x2, y1, y2, color);
}

Ok, so now you have your vertical and horizonal line drawing systems, and outlined
box func and a fill box func.

do this to get that effect you want:

(1)draw a filled box
(2)draw an outlined box in the same place over it.

It should work. Let me know if it does.

  1. Draw a circle

Don’t draw circles! I mean it!

If I could understand how that’s done, I can probably start writing almost-
good programs :wink:

Now write DAMN good programs NOW! And tell me about them! I had a lot of trouble
understanding writing to the screen myself so you are not alone.


– Michael Samuel

Paul Lowe
xpaull at ultraviolet.org

Where did you get this from?

Paul Lowe
xpaull at ultraviolet.org

Matt Slot wrote:> >> 2) Draw a circle

  Uh.... ?  Anybody have a fast circle drawing algorithm?

Here’s a snippet for Breshenham’s circle algorithm:

void Circle(long centerx, long centery, long radius)
{
long d, y, x;

    // setup
    d = 3 - (2 * radius);
    x = 0;
    y = radius;

    // draw the thing
    while (y > x) {
            SetPixel(centerx + x,centery + y);
            SetPixel(centerx + y,centery + x);
            SetPixel(centerx - x,centery + y);
            SetPixel(centerx - y,centery + x);
            SetPixel(centerx + x,centery - y);
            SetPixel(centerx + y,centery - x);
            SetPixel(centerx - x,centery - y);
            SetPixel(centerx - y,centery - x);

            if (d < 0) {
                    d = d + (4 * x) + 6;
            } else {
                    d = d + 4 * (x - y) + 10;
                    y--;
            }
            x++;
    }

}

Matt

/* Matt Slot, Bitwise Operator * One box, two box, yellow box, blue box. *

Sam Lantinga wrote:

First off…don’t use that disgusting screenlib…avoid the yucky C++ altogether
and just do it yourself!

Amen. :slight_smile:

To draw a filled box do this:
void

Fill_Box (SDL_Surface * surface, int x1, int y1, int x2, int y2, char color)

Use SDL_FillRect() if you are developing with SDL 0.9.x – it’s much faster
than your average rectangle fill function, and uses hardware acceleration if
available.

Whoah! Way cool! I never knew you put in an accelerated rect drawer! HELL YES!

Paul Lowe
xpaull at ultraviolet.org>

See ya!
-Sam Lantinga (slouken at devolution.com)


Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Ok, this question is quite a simple one (I hope)…

I want to make a very simple widget set for a program I’m writing (eg. buttons
and writing to go on them), and I feel rather silly drawing a BMP for each
one :wink:

I read the example in the docs of writing a single pixel, but that didn’t give
me any (workable) ideas on how to draw to the screen.

Anyway, could somebody give me some examples of how to do the following things:

  1. Draw a square, and fill in with a different colour to the outline

  2. Draw a circle

If I could understand how that’s done, I can probably start writing almost-
good programs ;-)–
– Michael Samuel

Ok, this question is quite a simple one (I hope)…

I’m surprised there haven’t been more answers on this one. :slight_smile:

  1. Draw a square, and fill in with a different colour to the outline

Check out screenlib for an example on how to draw lines, then:

Call SDL_MapRGB() for the colors...
Draw horizontal lines for top and bottom of square
Draw vertical lines for right and left of square
Call SDL_FillRect() to fill in the center
  1. Draw a circle
Uh.... ?  Anybody have a fast circle drawing algorithm?

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

  1. Draw a circle

Uh… ? Anybody have a fast circle drawing algorithm?

Here’s a snippet for Breshenham’s circle algorithm:

void Circle(long centerx, long centery, long radius)
{
long d, y, x;

// setup
d = 3 - (2 * radius);
x = 0;
y = radius;

// draw the thing
while (y > x) {
	SetPixel(centerx + x,centery + y);
	SetPixel(centerx + y,centery + x);
	SetPixel(centerx - x,centery + y);
	SetPixel(centerx - y,centery + x);
	SetPixel(centerx + x,centery - y);
	SetPixel(centerx + y,centery - x);
	SetPixel(centerx - x,centery - y);
	SetPixel(centerx - y,centery - x);

	if (d < 0) {
		d = d + (4 * x) + 6;
	} else {
		d = d + 4 * (x - y) + 10;
		y--;
	}
	x++;
}

}

Matt

/* Matt Slot, Bitwise Operator * One box, two box, yellow box, blue box. *

First off…don’t use that disgusting screenlib…avoid the yucky C++ altogether
and just do it yourself!

Amen. :slight_smile:

To draw a filled box do this:
void

Fill_Box (SDL_Surface * surface, int x1, int y1, int x2, int y2, char color)

Use SDL_FillRect() if you are developing with SDL 0.9.x – it’s much faster
than your average rectangle fill function, and uses hardware acceleration if
available.

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

  1. Draw a circle

Uh… ? Anybody have a fast circle drawing algorithm?

Ok, I think I can live without circles if it comes to that ;-)On Wed, Oct 21, 1998 at 05:12:38PM -0700, Sam Lantinga wrote:


– Michael Samuel

Hey,

Is there some way to poll for last known mouse button status, or do I need to
trap the event to get button status?

thanx,
/dev—
// David E. Vandewalle |Weinberg’s Law: If builders built
// vandewal at skyblue.com | buildings the way programmers wrote
// david.e.vandewalle at lmco.com | programs, then thefirst woodpecker that
// | came along would destroy civilization.

Is there some way to poll for last known mouse button status, or do I need to
trap the event to get button status?

From SDL_mouse.h:
/*

  • Retrieve the current state of the mouse.
  • The current button state is returned, and x and y are set to the current
  • mouse cursor position.
    */
    SDL_DEFUN(Uint8, SDL_GetMouseState, (Uint16 *x, Uint16 *y))

Note that you have to run SDL_PollEvent() or SDL_WaitEvent() to update
the internal mouse state.

Also, x and/or y can be NULL. I should add that to the documentation.

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

^^^^^^^^^^^^^^^^^^^^^^^^
Uh, yeah! :slight_smile: I must have read that line 10 times and not caught that. Time
for more caffine.

/devOn 22-Oct-98 Sam Lantinga wrote:

  • The current button state is returned, and x and y are set to the current

// David E. Vandewalle |Weinberg’s Law: If builders built
// vandewal at skyblue.com | buildings the way programmers wrote
// david.e.vandewalle at lmco.com | programs, then thefirst woodpecker that
// | came along would destroy civilization.

:slight_smile: The button state, by the way, is a bitmask. See some of the test
programs for the exact format.

I need to document that a bit better too.

See ya!
-Sam Lantinga (slouken at devolution.com)> On 22-Oct-98 Sam Lantinga wrote:

  • The current button state is returned, and x and y are set to the current
    ^^^^^^^^^^^^^^^^^^^^^^^^
    Uh, yeah! :slight_smile: I must have read that line 10 times and not caught that. Time
    for more caffine.


Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Michael Samuel wrote:

Ok, this question is quite a simple one (I hope)…

I want to make a very simple widget set for a program I’m writing (eg. buttons
and writing to go on them), and I feel rather silly drawing a BMP for each
one :wink:

I read the example in the docs of writing a single pixel, but that didn’t give
me any (workable) ideas on how to draw to the screen.

Anyway, could somebody give me some examples of how to do the following things:

  1. Draw a square, and fill in with a different colour to the outline

  2. Draw a circle

If I could understand how that’s done, I can probably start writing almost-
good programs :wink:


– Michael Samuel

Hello,

I’m interesed in Widget too, and I think some SDL people are too, so why nopt
developping together
some good widgets, for games, like the one that are used in war2 or startraft ?–
Stephane Magnenat
stephane.magnenat at urbanet.ch

I’m interesed in Widget too, and I think some SDL people are too, so why nopt
developping together
some good widgets, for games, like the one that are used in war2 or startraft ?

Well, I’d be in for that :wink:

I was thinking more along the lines of sticking some buttons and popup
boxes on the screen, and putting a picture and some text on them…On Fri, Oct 23, 1998 at 10:59:02AM +0000, St?phane Magnenat wrote:


– Michael Samuel

Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Michael Samuel wrote:> On Fri, Oct 23, 1998 at 10:59:02AM +0000, St?phane Magnenat wrote:

I’m interesed in Widget too, and I think some SDL people are too, so why nopt
developping together
some good widgets, for games, like the one that are used in war2 or startraft ?

Well, I’d be in for that :wink:

I was thinking more along the lines of sticking some buttons and popup
boxes on the screen, and putting a picture and some text on them…


– Michael Samuel

Yes, ok. BY widget I mean :

  • Button :

  •  Button            *
    

  • popup box :

  • V * popup box *

if you click on V, a list will be display

  • List

  • Item 1 * ^ *
  • Item 2 * *
  • Item 3 * V *

-Text cointener

-Scroll bar

If someone is interested, I’m ready for ideas

Bets regards,


Stephane Magnenat
stephane.magnenat at urbanet.ch