Trying to make sense of sdl1.3 features so i can make simple tutorial - IDW - ( also i am an idiot )

anyone can twak this code so it behaves for me? i dont even know if it
should behave!

hopefully if i get my head round this i can make simple tutorial for lazy
azses like me

/* The general idea of this bit of code will be (hopefully is ) to simply
demonstrate dual windows with SDL
as i have no clue if this feature set works or how to work it may be an
issue*/

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include “SDL_events.h”
#include “SDL_video.h”

#define NUM_SPRITES 100
#define MAX_SPEED 1

static SDL_Texture *sprite;
static SDL_Rect positions[2][NUM_SPRITES];
static SDL_Rect velocities[2][NUM_SPRITES];
static int sprite_w, sprite_h;

SDL_Window *window[2];

/* first create a struct def to hold our screens data might as well be a
SDL_Rect at this point */

typedef struct {
int x;
int y;
int w;
int h;
} ScrConfig;

/* now define some screens */

/* done be a dummy and remebr 2 mean 0 and 1 */
ScrConfig scrconfig[2];

/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
exit(rc);
}

int
LoadSprite(char *file)
{
SDL_Surface *temp;

/* Load the sprite image */
temp = SDL_LoadBMP(file);
if (temp == NULL) {
    fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    return (-1);
}
sprite_w = temp->w;
sprite_h = temp->h;

/* Set transparent pixel as the pixel at (0,0) */
if (temp->format->palette) {
    SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
} else {
    switch (temp->format->BitsPerPixel) {
    case 15:
        SDL_SetColorKey(temp, SDL_TRUE,
                        (*(Uint16 *) temp->pixels) & 0x00007FFF);
        break;
    case 16:
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
        break;
    case 24:
        SDL_SetColorKey(temp, SDL_TRUE,
                        (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
        break;
    case 32:
        SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
        break;
    }
}

/* Create textures from the image */
sprite = SDL_CreateTextureFromSurface(0, temp);
if (!sprite) {
    SDL_SetColorKey(temp, 0, 0);
    sprite = SDL_CreateTextureFromSurface(0, temp);
}
if (!sprite) {
    fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    SDL_FreeSurface(temp);
    return (-1);
}
SDL_FreeSurface(temp);

/* We're ready to roll. :) */
return (0);

}

void
MoveSprites(int win, SDL_Texture * sprite)
{
int i, n;
int window_w, window_h;
SDL_Rect temp;
SDL_Rect *position, *velocity;

SDL_SelectRenderer(window[win]);

/* Query the sizes */
SDL_GetWindowSize(window[win], &window_w, &window_h);


/* Move the sprite, bounce at the wall, and draw */
n = 0;
for (i = 0; i < NUM_SPRITES; ++i) {
    position = &positions[win][i];
    velocity = &velocities[win][i];
    position->x += velocity->x;
    if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
        velocity->x = -velocity->x;
        position->x += velocity->x;
    }
    position->y += velocity->y;
    if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
        velocity->y = -velocity->y;
        position->y += velocity->y;
    }

    /* Blit the sprite onto the screen */
    SDL_RenderCopy(sprite, NULL, position);
}

/* Update the screen! */
SDL_RenderPresent();

}

int
main(int argc, char *argv[])
{

scrconfig[0].x=10;
scrconfig[0].y=10;
scrconfig[0].w=200;
scrconfig[0].h=300;

scrconfig[1].x=400;
scrconfig[1].y=10;
scrconfig[1].w=300;
scrconfig[1].h=200;

int i, done;
SDL_Event event;


   window[0] =

SDL_CreateWindow(“WINDOW1”,scrconfig[0].x,scrconfig[0].y,scrconfig[0].w,scrconfig[0].h,SDL_WINDOW_SHOWN|SDL_WINDOW_INPUT_FOCUS|SDL_WINDOW_RESIZABLE|SDL_WINDOW_MAXIMIZED|SDL_WINDOW_BORDERLESS);

SDL_CreateRenderer(window[0],-1,SDL_RENDERER_SINGLEBUFFER|SDL_RENDERER_ACCELERATED);
SDL_SelectRenderer(window[0]);
SDL_SetRenderDrawColor(0,0,0,0);
SDL_RenderClear();
SDL_RenderPresent();

   window[1] =

SDL_CreateWindow(“WINDOW2”,scrconfig[1].x,scrconfig[1].y,scrconfig[1].w,scrconfig[1].h,SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE|SDL_WINDOW_MAXIMIZED|SDL_WINDOW_BORDERLESS);

SDL_CreateRenderer(window[1],-1,SDL_RENDERER_SINGLEBUFFER|SDL_RENDERER_ACCELERATED);
SDL_SelectRenderer(window[1]);
SDL_SetRenderDrawColor(0,0,0,0);
SDL_RenderClear();
SDL_RenderPresent();

if (!window[1] ) {
    quit(2);
}

 if (!window[0] ) {
    quit(2);
}


if (LoadSprite("icon.bmp") < 0) {
    quit(2);
}

/* Initialize the sprite positions in each window */
srand(time(NULL));
int w,window_w, window_h;
for (w=0 ; w<2 ; w++)
{
     SDL_GetWindowSize(window[w], &window_w, &window_h);
for (i = 0; i < NUM_SPRITES; ++i) {
    positions[w][i].x = rand() % (window_w - sprite_w);
    positions[w][i].y = rand() % (window_h - sprite_h);
    positions[w][i].w = sprite_w;
    positions[w][i].h = sprite_h;
    velocities[w][i].x = 0;
    velocities[w][i].y = 0;
    while (!velocities[w][i].x && !velocities[w][i].y) {
        velocities[w][i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
        velocities[w][i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
    }
}
}

/* Main render loop */
done = 0;
while (!done) {
    /* Check for events */
    while (SDL_PollEvent(&event)) {
        if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
            done = 1;
        }
    }
    MoveSprites(0, sprite);
MoveSprites(1, sprite);
}

quit(0);

}

/* vi: set ts=4 sw=4 expandtab: */

You’re not calling SDL_Init and SDL_Quit.On 12 January 2011 07:40, Neil White wrote:

anyone can twak this code so it behaves for me? i dont even know if it
should behave!

hopefully if i get my head round this i can make simple tutorial for lazy
azses like me

Consider using the SDL test programs as a reference.

Also, reconsider if you are in a position to write an authorative
tutorial if you’re having these kind of problems with the API and
cannot solve them yourself.

HTH,
– Brian.On 12 January 2011 12:40, Neil White wrote:

anyone can twak this code so it behaves for me? i dont even know if it
should behave!

hopefully if i get my head round this i can make simple tutorial for lazy
azses like me

Those who are “in a position to write an authoritative tutorial” have
not yet done so. Consider what the state of SDL would be if only the
"authorities" contributed to it.On Wed, Jan 12, 2011 at 9:32 AM, Brian Barrett <brian.ripoff at gmail.com> wrote:

Consider using the SDL test programs as a reference.

Also, reconsider if you are in a position to write an authorative
tutorial if you’re having these kind of problems with the API and
cannot solve them yourself.

HTH,
? – ?Brian.

On 12 January 2011 12:40, Neil White wrote:

anyone can twak this code so it behaves for me? i dont even know if it
should behave!

hopefully if i get my head round this i can make simple tutorial for lazy
azses like me


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

so i just crack SDL_init on one end and a SDL_Quit an the other and its
gonna be fine right?

i did think about using cmake see if that helped

I said “authorative” (though I actually meant “authoratative”), not
"official". By that I meant “using an authoratative voice”, as in
acting like you know your stuff when in fact you may only have an
incomplete understanding of it (Google definition of authoratative:
Able to be trusted as being accurate or true; reliable: “clear,
authoritative information”).

My point is that the OP might yet be underqualified to write such a
tutorial in the near future if they cannot independently solve this
kind of problem.

The internet is full of poor tutorials written by people who have
literally just started to learn a language and/or API. While I take
the point that experienced individuals rarely take up the slack and
produce high quality tutorials (an example would be Lazy Foo’s SDL 1.2
tutorials, which are quite good), I don’t think that weaker tutorials
are a good substitute.

Finally, all I said was that the OP “reconsider”, I didn’t say they
should not do it. Maybe waiting until they have written a full game
using the API rather than when they get something that compiles and
runs and appears to be correct.On 13 January 2011 01:43, Justin Coleman wrote:

Those who are “in a position to write an authoritative tutorial” have
not yet done so. Consider what the state of SDL would be if only the
"authorities" contributed to it.

so can anyone read this code and help out or do i just sit here reading your
witterings, any resource on the internet about something like sdl that dosnt
have a large knowledge base, even if it is screwed and a bit wrong or bad
practise, i have never used any of the larger tutorials about i have botched
hacked strained and peered ant screen lost sleep worked it all out and am
better for it, thats what i will be doing with this code, if my suffering
can help someone else then all good, and thats it, you wanna b1tch about who
should be writing tutorials make a new thread. i cant cope :wink:

happy holidays!

only being silly, you rant all you want, someone to bother reading my code
would be a bonus tho

I recommend there be a google code page called sdl13-tutorial where we can
mirror the sdl repository and have wiki pages dedicated to community
tutorials. That way, everyone can contribute and it acts as the community
authoritative voice. I’ve taken the liberty to start the project page here:
http://code.google.com/p/sdl-13-tutorials/
Please let me know if you want to contribute

-OzOn Thu, Jan 13, 2011 at 11:31 AM, Neil White wrote:

so can anyone read this code and help out or do i just sit here reading
your witterings, any resource on the internet about something like sdl that
dosnt have a large knowledge base, even if it is screwed and a bit wrong or
bad practise, i have never used any of the larger tutorials about i have
botched hacked strained and peered ant screen lost sleep worked it all out
and am better for it, thats what i will be doing with this code, if my
suffering can help someone else then all good, and thats it, you wanna b1tch
about who should be writing tutorials make a new thread. i cant cope :wink:

happy holidays!

only being silly, you rant all you want, someone to bother reading my code
would be a bonus tho


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

That sounds fun. Let me in. :slight_smile:

Jonny DOn Thu, Jan 13, 2011 at 11:52 AM, Alex Barry <alex.barry at gmail.com> wrote:

I recommend there be a google code page called sdl13-tutorial where we can
mirror the sdl repository and have wiki pages dedicated to community
tutorials. That way, everyone can contribute and it acts as the community
authoritative voice. I’ve taken the liberty to start the project page here:
http://code.google.com/p/sdl-13-tutorials/
Please let me know if you want to contribute

-Oz

On Thu, Jan 13, 2011 at 11:31 AM, Neil White wrote:

so can anyone read this code and help out or do i just sit here reading
your witterings, any resource on the internet about something like sdl that
dosnt have a large knowledge base, even if it is screwed and a bit wrong or
bad practise, i have never used any of the larger tutorials about i have
botched hacked strained and peered ant screen lost sleep worked it all out
and am better for it, thats what i will be doing with this code, if my
suffering can help someone else then all good, and thats it, you wanna b1tch
about who should be writing tutorials make a new thread. i cant cope :wink:

happy holidays!

only being silly, you rant all you want, someone to bother reading my code
would be a bonus tho


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

John, addedOn Thu, Jan 13, 2011 at 12:04 PM, Jonathan Dearborn wrote:

That sounds fun. Let me in. :slight_smile:

Jonny D

On Thu, Jan 13, 2011 at 11:52 AM, Alex Barry <@Alex_Barry> wrote:

I recommend there be a google code page called sdl13-tutorial where we can
mirror the sdl repository and have wiki pages dedicated to community
tutorials. That way, everyone can contribute and it acts as the community
authoritative voice. I’ve taken the liberty to start the project page here:
http://code.google.com/p/sdl-13-tutorials/
Please let me know if you want to contribute

-Oz

On Thu, Jan 13, 2011 at 11:31 AM, Neil White wrote:

so can anyone read this code and help out or do i just sit here reading
your witterings, any resource on the internet about something like sdl that
dosnt have a large knowledge base, even if it is screwed and a bit wrong or
bad practise, i have never used any of the larger tutorials about i have
botched hacked strained and peered ant screen lost sleep worked it all out
and am better for it, thats what i will be doing with this code, if my
suffering can help someone else then all good, and thats it, you wanna b1tch
about who should be writing tutorials make a new thread. i cant cope :wink:

happy holidays!

only being silly, you rant all you want, someone to bother reading my
code would be a bonus tho


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

It’s a start at least.

Do you have any experience with the older 1.2 API?On 13/01/2011, Neil White wrote:

so i just crack SDL_init on one end and a SDL_Quit an the other and its
gonna be fine right?