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: */