Can I plot individual pixels using SDL?

I have a screen saver program that plots mathematical functions (using graphics from TurboC). The graphics is really slow. I have SDL2 library installed on my Ubuntu computer and have tinkered with an SDL tutorial - I didn’t see anything in the commands described in the SDL online manual that plots individual points. Does SDL have capability to plot points? If so please tell me what commands to use. TIA. Bill S.

You would use SDL_RenderDrawPoint for rendering to a texture/screen

Additionally you can use SDL_Surface as a pixel array and manipulate any pixel you want. After this manipulations draw this surface.

I wrote a program over 8 years ago that plots polynomials (up to 7th order)
and their derivatives (up to the 4th) and displays them quickly. If your
screen saver is animated, though, it may not be fast enough. If you like, let
me know and I’ll send you the source directly.

It’s written in C++ and uses SDL 1.2, but should easily adapt to SDL2.

If all you need is a function that plots individual points, the code used is

// Set pixel at given coordinates to the given color

void setPixel(int x, int y, int color)
{
Uint8 *row8;
Uint16 *row16;
Uint32 *row32;

if (x < 0 || x >= SCREEN_PIXEL_WIDTH || y < 0 || y >= 

SCREEN_PIXEL_HEIGHT)
return; // Don’t allow overwriting boundaries of the screen

switch (bytes_per_pixel)
{
	case 1:
		row8 = (Uint8 *) (screen->pixels + y * pitch + x * 

bytes_per_pixel);
*row8 = (Uint8) color;
break;

	case 2:
		row16 = (Uint16 *) (screen->pixels + y * pitch + x * 

bytes_per_pixel);
*row16 = (Uint16) color;
break;

	case 4:
		row32 = (Uint32 *) (screen->pixels + y * pitch + x * 

bytes_per_pixel);
*row32 = (Uint32) color;
break;
}
}

The ‘screen’ variable above is a pointer to an SDL_Surface.

JeffOn Saturday 28 February 2015 07:33:08 bilsch01 wrote:

I have a screen saver program that plots mathematical functions (using
graphics from TurboC). The graphics is really slow. I have SDL2 library
installed on my Ubuntu computer and have tinkered with an SDL tutorial - I
didn’t see anything in the commands described in the SDL online manual
that plots individual points. Does SDL have capability to plot points?
If so please tell me what commands to use. TIA. Bill S.

MrTAToad wrote:

You would use SDL_RenderDrawPoint for rendering to a texture/screen

I can run other SDL programs so I think I have the SDL libraries I need. But when I put SDL_RenderDrawPoint in the program I get the following error:

sdlsnx2.c:(.text+0x192): undefined reference to `SDL_RenderDrawPoint’
collect2: error: ld returned 1 exit status

Is there some other library or header file I need? I notice the command is not included in the reference pages at:
http://www.libsdl.org/release/SDL-1.2.15/docs/html/. Apparently that site is missing some commands. Can you tell me a site that has complete set of SDL commands? Thanks.

http://libsdl.org/download-2.0.php
http://wiki.libsdl.org/

Message-ID: <1425137588.m2f.47057 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

I have a screen saver program that plots mathematical functions (using graphics from TurboC). The graphics is really slow. I have SDL2 library installed on my Ubuntu computer and have tinkered with an SDL tutorial - I didn’t see anything in the commands described in the SDL online manual that plots individual points. Does SDL have capability to plot points? If so please tell me what commands to use. TIA. Bill S.

You want to use either the renderer with the SDL_Render* functions, or
an SDL_Surface with a hand-written pixel manipulation function. Only
you can decide which is the correct choice.> Date: Sat, 28 Feb 2015 15:33:08 +0000

From: “bilsch01”
To: sdl at lists.libsdl.org
Subject: [SDL] Can I plot individual pixels using SDL?

Message-ID: <201502281543.15047.j_post at pacbell.net>
Content-Type: Text/Plain; charset=“iso-8859-6”

I have a screen saver program that plots mathematical functions (using
graphics from TurboC). The graphics is really slow. I have SDL2 library
installed on my Ubuntu computer and have tinkered with an SDL tutorial - I
didn’t see anything in the commands described in the SDL online manual
that plots individual points. Does SDL have capability to plot points?
If so please tell me what commands to use. TIA. Bill S.

I wrote a program over 8 years ago that plots polynomials (up to 7th order)
and their derivatives (up to the 4th) and displays them quickly.

It’s written in C++ and uses SDL 1.2, but should easily adapt to SDL2.

If all you need is a function that plots individual points, the code used is

// Set pixel at given coordinates to the given color

void setPixel(int x, int y, int color)
{

Jeff

If you don’t have an objection, then I’m going to use this for my own
textured-triangles code.> Date: Sat, 28 Feb 2015 15:43:14 -0800

From: “j_post” <j_post at pacbell.net>
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Can I plot individual pixels using SDL?
On Saturday 28 February 2015 07:33:08 bilsch01 wrote:

Message-ID: <1425171479.m2f.47082 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

MrTAToad wrote:

You would use SDL_RenderDrawPoint for rendering to a texture/screen

I can run other SDL programs so I think I have the SDL libraries I need. But when I put SDL_RenderDrawPoint in the program I get the following error:

sdlsnx2.c:(.text+0x192): undefined reference to `SDL_RenderDrawPoint’
collect2: error: ld returned 1 exit status

Is there some other library or header file I need? I notice the command is not included in the reference pages at:
http://www.libsdl.org/release/SDL-1.2.15/docs/html/. Apparently that site is missing some commands. Can you tell me a site that has complete set of SDL commands? Thanks.

Are you COMPLETELY certain that you’re using SDL 2? The reference link
that you posted is for SDL 1.2, which is one of the predecessor
versions, but actually doesn’t have API or ABI compatibility with SDL
2.

If for some reason you need to use SDL 1.2, then you’ll need to go
with the software-surface method that Jeff mentioned.> Date: Sun, 01 Mar 2015 00:57:59 +0000

From: “bilsch01”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Can I plot individual pixels using SDL?

No objection at all, glad it’s of use to you or anyone else.

JeffOn Saturday 28 February 2015 21:03:50 Jared Maddox wrote:

// Set pixel at given coordinates to the given color

void setPixel(int x, int y, int color)
{

Jeff

If you don’t have an objection, then I’m going to use this for my own
textured-triangles code.

[/quote]
Are you COMPLETELY certain that you’re using SDL 2? The reference link
that you posted is for SDL 1.2, which is one of the predecessor
versions, but actually doesn’t have API or ABI compatibility with SDL
2.

If for some reason you need to use SDL 1.2, then you’ll need to go
with the software-surface method that Jeff mentioned.

The SDL2 library is in my system however I think my capability is limited to 1.2 because the compiler says UNDEFINED REFERENCE to SDL_RenderDrawPoint. I don’t see that the code posted by Jeff will plot a pixel. My code computes x and y coordinates for 640x480 display and uses TurboC putpixel() to display the computed point. putpixel is very slow. SDL_RenderDrawPoint is not available apparently because I have SDL 1.2 capability only. Apparently there is no command in SDL 1.2 that displays individual pixels.

bilsch01 wrote:

The SDL2 library is in my system however I think my capability is limited to 1.2 because the compiler says UNDEFINED REFERENCE to SDL_RenderDrawPoint.

You should correctly add paths to your include and libs files when compiling and linking your app.
Try this console commands:
sdl2-config --cflags
sdl2-config --libs
Is this commands show something on your system?

I don’t see that the code posted by Jeff will plot a
pixel.

It sets the pixel in the SDL_Surface pointed to by ‘screen’. At some point you
need to call SDL_UpdateRect to actually display the changes, but you don’t
want to do that after every call to setPixel or it will be real slow.

What my code (call ‘polygraph’ for polynomial graphing) does is plot the
entire function within the applicable domain and range using calls to setPixel
and only then calls SDL_UpdateRect. I’ve never actually timed how long it
takes since once it begins plotting, the curve appears on screen faster than a
human can detect. Good enough for my uses.

If, on the other hand, by ‘plot a pixel’ you mean knowing which pixel to set
to what color, that’s the job of your application code.

Again, if you’d like the program’s source code (which is GPL’d) I’ll send you
a zip or tar.gz file, but I see no need to burden the mailing list with it. It
will compile for either Linux or Windows, but not OSX, 'cause, you know, Apple
sucks :wink:

My code computes x and y coordinates for 640x480 display and uses
TurboC putpixel() to display the computed point. putpixel is very slow.

It’s been ages since I’ve used TurboC so I can’t say for sure, but it could be
that the TurboC library is updating the displayed data (the physical screen)
after every call to putpixel(). See above.

JeffOn Sunday 01 March 2015 05:46:34 bilsch01 wrote:

Jared Maddox wrote:

If you don’t have an objection, then I’m going to use this for my own
textured-triangles code.

Jared, if you plan to publish your textured-triangles code under conditions of zlib license, like SDL2 library license, then you can’t use that code because it under GPL license. This two licenses are incompatible. I think you can use zlib-licensed code in GPL-ed application, but you can’t use GPL-ed code in zlib-licensed app or library. Be careful with this licenses.

The application program is GPL. Anyone has my permission to use the setPixel
routine in their software under any license they choose as long as said
license doesn’t try to make the code proprietary. Same applies to any function
in the program that interfaces to SDL functions or structures, as opposed to
the higher level application code which must remain GPL.

I’m no lawyer (I have ethics :-), but the above statement is legally binding.On Sunday 01 March 2015 08:31:00 Alex wrote:

Jared Maddox wrote:

If you don’t have an objection, then I’m going to use this for my own
textured-triangles code.

Jared, if you plan to publish your textured-triangles code under conditions
of zlib license, like SDL2 library license, then you can’t use that code
because it under GPL license. This two licenses are incompatible. I think
you can use zlib-licensed code in GPL-ed application, but you can’t use
GPL-ed code in zlib-licensed app or library. Be careful with this
licenses.


| Application | GPL

(bunch of math stuff)
       |
       V

| SDL interface | any license you want that is not proprietary

          |
          V

| SDL | SDL’s license

Jeff

j_post wrote:

The application program is GPL. Anyone has my permission to use the setPixel
routine in their software under any license they choose as long as said
license doesn’t try to make the code proprietary.

Thanks, Jeff.
I’m not lawyer too, but I have some (limited) knowledge about licenses :slight_smile:
Cause you can use SDL2 in your GPL application - zlib-license permit you to use it.
Zlib-license is much more permissive than GPL. Any code licensed under zlib conditions can be used even inside of proprietary application (not only in free apps). If you forbid to use this code in proprietary application, then this code can’t be licensed under zlib conditions.

You right, this scheme is correct:
|GPL App| (use/include code)-> |SDL2|

This scheme is correct too:
|Proprietary app| (use/include code)-> |SDL2|

This scheme is not correct:
|SDL2| (use/include code)-> |GPL code|

I mean you can’t use GPL code inside SDL2 library. But you can use SDL2 code even in proprietary app or lib.

Sorry for offtopic. This topic is not about licenses.

Since I wrote that app more than 8 years ago, it uses SDL1.2. If anyone cares
to port it to SDL2, please let me know. I’d love to have an SDL2 version of
it, but I lack the time to port it myself. (It’s written in C++, but much of
it is very C-ish.)

JeffOn Sunday 01 March 2015 09:33:22 Alex wrote:

j_post wrote:

The application program is GPL. Anyone has my permission to use the
setPixel routine in their software under any license they choose as long
as said license doesn’t try to make the code proprietary.

Thanks, Jeff.
I’m not lawyer too, but I have some (limited) knowledge about licenses :slight_smile:
Cause you can use SDL2 in your GPL application - zlib-license permit you to
use it.

j_post wrote:

Since I wrote that app more than 8 years ago, it uses SDL1.2. If anyone cares
to port it to SDL2, please let me know. I’d love to have an SDL2 version of
it, but I lack the time to port it myself. (It’s written in C++, but much of
it is very C-ish.)

Jeff

You still can use SDL_Surface to draw in SDL2 (http://wiki.libsdl.org/SDL_Surface). I think it is not very hard work to port your code to SDL2. Just rewrite some small parts of code :slight_smile:

Message-ID: <1425217594.m2f.47095 at forums.libsdl.org>
Content-Type: text/plain; charset=“iso-8859-1”

[/quote]
Are you COMPLETELY certain that you’re using SDL 2? The reference link
that you posted is for SDL 1.2, which is one of the predecessor
versions, but actually doesn’t have API or ABI compatibility with SDL
2.

If for some reason you need to use SDL 1.2, then you’ll need to go
with the software-surface method that Jeff mentioned.

The SDL2 library is in my system however I think my capability is limited to 1.2 because the compiler says UNDEFINED REFERENCE to SDL_RenderDrawPoint. I don’t see that the code posted by Jeff will plot a pixel. My code computes x and y coordinates for 640x480 display and uses TurboC putpixel() to display the computed point. putpixel is very slow. SDL_RenderDrawPoint is not available apparently because I have SDL 1.2 capability only. Apparently there is no command in SDL 1.2 that displays individual pixels.

Time for a history lesson.

Once upon a time ( specifically, before the mid-90s), programs
accessed the display card directly. This worked in the following
manner: the card would have a block of memory, and that block of
memory could be accessed by the CPU as if it was part of the
computer’s normal memory. The display card would in turn also be
accessing that block of memory, through a physically separate set of
access pins (this type of memory is called “dual-ported”, and still
exists for this type of application), and use the information that it
obtained to control the actual display. The arrangement of this memory
was that of a set of one-dimensional matrixes, arranged sequentially
in memory, with the elements of each array also in sequential order,
with whatever padding was necessary inside or between the arrays. Text
displays would have a byte-per-character + a separate ROM to contain
the appearance of the actual characters, black and white displays
would usually have a bit-per-pixel, “True-color” displays would (and
technically still do, for the sake of boot-stage graphics and
backwards-compatibility) usually have either 3 bytes or 4 bytes per
pixel, etc.

To access the space of an individual pixel you would determine the X
and Y coordinates of the pixel, calculate the affect of bit-alignment
on the location (we don’t do this with SDL, because SDL doesn’t do
pixels that are a non-whole multiple of a byte), and then do some
multiplication and addition to determine the location in memory of the
pixel.

SDL’s surfaces work with this exact same scheme. The “pitch” variable
in Jeff’s code is the distance between the beginning of adjacent
one-dimensional pixel matrixes (called scanlines): this number takes
into account both the space occupied by the pixels, as well as any
padding.

The TurboC putpixel() function most likely works very similarly, since
Windows started using “dib” (basically the in-memory form of a .bmp
file) images in it’s rendering system very early.

SDL 1.2 (and 2, though for more recent graphics models) is basically
built around providing a platform-independent interface to graphics
that’s compatible with common rendering practices, so the natural
assumption going in was that the pixel access code would already be
there (and probably be optimized already), and just need to be
modified instead of being written from scratch. Thus, it didn’t
actually make sense to include pixel access routines.> Date: Sun, 01 Mar 2015 13:46:34 +0000

From: “bilsch01”
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Can I plot individual pixels using SDL?

Message-ID: <201503010910.30569.j_post at pacbell.net>
Content-Type: Text/Plain; charset=“iso-8859-6”

Jared Maddox wrote:

If you don’t have an objection, then I’m going to use this for my own
textured-triangles code.

Jared, if you plan to publish your textured-triangles code under conditions
of zlib license, like SDL2 library license, then you can’t use that code
because it under GPL license. This two licenses are incompatible. I think
you can use zlib-licensed code in GPL-ed application, but you can’t use
GPL-ed code in zlib-licensed app or library. Be careful with this
licenses.

The application program is GPL. Anyone has my permission to use the setPixel
routine in their software under any license they choose as long as said
license doesn’t try to make the code proprietary.

I was going to point out that Alex’s point about this still not being
compatible was right (since SDL 2 is allowed to be static-linked, the
code in it can become INDIRECTLY subject to a license making the code
proprietary, thereby conflicting with your terms), but then I looked
at the code and noticed it doesn’t actually calculate the pixel value
from raw RGB(A) values, so it isn’t actually useful to me anyways :frowning: .

I’ll just go digging around in the Hg for a while to see if I can find
something.> Date: Sun, 1 Mar 2015 09:10:30 -0800

From: “j_post” <j_post at pacbell.net>
To: sdl at lists.libsdl.org
Subject: Re: [SDL] Can I plot individual pixels using SDL?
On Sunday 01 March 2015 08:31:00 Alex wrote: