Sprite Questions (Parallax)

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
speewave
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 18
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Sun Feb 28, 2010 9:29 pm
Has thanked: 0
Been thanked: 0

Sprite Questions (Parallax)

Post by speewave »

I'm trying to do sprites in Parallax...

i've learned how to Load a sprite in the middle of the frame and display a texture...

==========================
now how do i do multiple sprites..

My code was a modified version of Raster_Melt in the parallax examples in KOS. but using a sprite instead of a custom vertex... i loaded the sprite, created the sprite context and sent it over to the PVR, set the sprite up, and rendered the scene..


sorry i don't have the code... my dev computer has no internet
speewave
DCEmu Fast Newbie
DCEmu Fast Newbie
Posts: 18
Joined: Sun Feb 28, 2010 9:29 pm
Has thanked: 0
Been thanked: 0

Re: Sprite Questions (Parallax)

Post by speewave »

also how do you work with matrix movements (Translation\Rotation\Scale) with plx_sprite_fmp
User avatar
BB Hood
DC Developer
DC Developer
Posts: 189
Joined: Fri Mar 30, 2007 12:09 am
Has thanked: 41 times
Been thanked: 10 times

Re: Sprite Questions (Parallax)

Post by BB Hood »

viewtopic.php?f=29&t=83923&start=0&st=0 ... x+rotation

I used to work a lot with the parallax library but forgot how it works since I haven't really programmed at all for a long time. If no body answers this I will tell you within a week.

Me Ranting - Being a beginner you should experiment with library functions. It really helps you to understand the library you're using and helps you not rely on others whenever you get stuck. That is what I did so I didn't have to create many threads and ask a lot of questions. I only used these boards as a last resort(most of the time). But I always used the search function first before creating a new thread. You should also save source codes from previous dreamcast projects (that are open source) that you come across so you can turn to them when you need help (If you need to mimic an effect they did whether it be visual or audio). I recommend you download the feet of fury source code. It deals a lot with plx functions since it used the tsunami library. That is where I started to understand how a library works and how plx graphic functions are implemented. Once you understand that(plx graphic functions) you can concentrate on how plx handles graphics using the PVR and ultimately use the pvr directly (which, for me, lead to the porting of DC Mappy from SDL Mappy). Oh and no double posting. Edit your first post instead.

You can find the link for the source on this page:
http://www.dcevolution.net/index.php?id=feet_of_fury

Scroll down all the way. It's under source code.

EDIT:

By "multiple sprites" I am guessing you mean animations. I haven't really dealt with animations all that much but I have a reference in front of me (Game Programming All-in-one 3rd Ed.) so I will use the author's(Jonathan S. Harbour) source and modify it so it uses Parallax instead of Allegro.

First we have the Sprite header file (Sprite.h):

Code: Select all

#ifndef SPRITE_H
#define SPRITE_H

#include <kos.h>
#include <plx/list.h>
#include <plx/prim.h>
#include <plx/color.h>
#include <plx/texture.h>
#include <plx/context.h>

class Sprite {

private:
    int alive;
    int state;
    int list; // PLX_LIST_OP_POLY, PLX_LIST_TR_POLY, or PLX_LIST_PT_POLY
    int objecttype;
    int direction;
    double x,y;
    int width, height;
    double xspeed, yspeed;
    int xdelay, ydelay;
    int xcount, ycount;
    int curframe, totalframes, animdir;
    int framecount, framedelay;
    int animcolumns;
    int animstartx, animstarty;
    int faceAngle, moveAngle;
    plx_texture *image;

public:
    Sprite(void);
    ~Sprite();
    int load(char * filename, int listtype);
    int getWidth(void) { return width };
    int getHeight(void) { return height };
    double getXposition(void) { return x };
    double getYposition(void) { return y };
    void setXposition(double setx);
    void setYposition(double sety);
    void setXspeed(double xspd);
    void setYspeed(double yspd);
    void setAnimDir(int dir);
    void draw(int listtype);
    void drawframe(void);
    void updatePosition(void);
    void updateAnimation(void);
    
};
#endif
Now the implementation file (Sprite.cpp):

Code: Select all

#include "Sprite.h"

Sprite::Sprite() {
    image = NULL;
    alive = 1;
    direction = 0;
    animcolumns = 0;
    animstartx = 0;
    animstarty = 0;
    x = 0.0f;
    y = 0.0f;
    width = 0;
    height = 0;
    xdelay = 0;
    ydelay = 0;
    xcount = 0;
    ycount = 0;
    xspeed = 0.0;
    yspeed = 0.0;
    speed = 0.0;
    curframe = 0;
    totalframes = 1;
    framecount = 0;
    framedelay = 10;
    animdir = 1;
    faceAngle = 0;
    moveAngle = 0;
}

Sprite::~Sprite() {
    // Remove bitmap from memory
    if(image != NULL) {
        plx_txr_destroy(image);
    }
}

int Sprite::load(char *filename, int listtype) {
    image = plx_txr_load(filename, 1, PVR_TXRLOAD_16BPP);
    if (image == NULL) { return -1; }
    width = image->w; 
    height = image->h;
    list = listtype;
    return 1;
}

void Sprite::setXposition(double setx) {
    x = setx;
}

void Sprite::setYposition(double sety) {
    y = sety;
}

void Sprite::setXspeed(double xspd) {
    xspeed = xspd; 
}

void Sprite::setYspeed(double xspd) {
    yspeed = yspd;
}

void Sprite::setAnimDir(int dir) {
    animdir = dir;
}

void Sprite::draw(int listtype) {
    if (list != typelist) { return; }

    plx_cxt_texture(image);
    plx_txr_send_hdr(image, list, 0);

    plx_vertex_t vert;
    if (list == PLX_LIST_TR_POLY) {
        vert.argb = getColor();
    } else {
        Color t = getColor(); t.a = 1.0f;
        vert.argb = t;
    }
    vert.oargb = 0;

    vert.flags = PLX_VERT;
    vert.x = x;
    vert.y = y;
    vert.z = 1;
    vert.u = 0.0;
    vert.v = 0.0;
    plx_prim(&vert, sizeof(vert));

    vert.x = x + width;
    vert.u = 1.0;
    plx_prim(&vert, sizeof(vert));

    vert.x = x;
    vert.y = y + height;
    vert.u = 0.0;
    vert.v = 1.0;
    plx_prim(&vert, sizeof(vert));

    vert.flags = PLX_VERT_EOS;
    vert.x = x + width;
    vert.u = 1.0;
    plx_prim(&vert, sizeof(vert));
}

void Sprite::drawframe() {
    int fx = animstartx + (curframe % animcolumns) * width;
    int fy = animstarty + (curframe / animcolumns) * height;
    ... finish later
} 

void::updatePosition() {
    //update x position
    if(++xcount > xdelay) {
        xcount = 0;
        x += xspeed;
    }

    //update y position
    if(++ycount > ydelay) {
        ycount = 0;
        y += yspeed;
    }
}

void Sprite::updateAnimation() {
    //update frame based on animdir
    if(++framecount > framedelay) {
        framecount = 0;
        curframe += animdir;
        if(curframe < 0) {
            curframe = totalframes - 1;
        }
        if(curframe > totalframes - 1) {
            curframe = 0;
        }
    }
}
Now a Sprite handler class for convenience. Its a "storage bin where the sprites are stored together" (Harbour, John, pg393).

SpriteHandler.h:

Code: Select all

class SpriteHandler {
private:
    int count;
    Sprite *sprites[100];
public:
    SpriteHandler(void);
    ~SpriteHandler(void);
    void add(Sprite *spr);
    void create();
    Sprite *get(int index);
    int size() { return count; }
};
SpriteHandler.cpp:

Code: Select all

#include "SpriteHandler.h"

SpriteHandler::SpriteHandler(void) {
    count = 0;
}

SpriteHandler::~SpriteHandler(void) {
    //delete the sprites
    for (int n = 0; n < count; n++)
        delete sprites[n];
}

void SpriteHandler::add(Sprite *spr) {
    if(spr != NULL) {
        sprites[count] = spr;
        count++;
    }
}

void SpriteHandler::create() {
    sprites[count] = new sprite();
    count++;
}

Sprite *SpriteHandler::get(int index) {
    return sprites[index];
}
This code is not tested and the drawframe() function wont work because it is not finished. I will finish it later and maybe come up with an example but you should get some idea on how to do animations by looking at this code. The drawframe() will draw a specific section of an image. The most convenient way to draw animations is from one image file that has all the sprites of an animation in it.
Post Reply