Source code: com/siemens/mp/game/Sprite.java
1 /*
2 * Siemens API for MicroEmulator
3 * Copyright (C) 2003 Markus Heberling <markus@heberling.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package com.siemens.mp.game;
20
21 import javax.microedition.lcdui.*;
22 /**
23 *
24 * @author markus
25 * @version
26 */
27 public class Sprite extends GraphicObject{
28 Image pixels[];
29 Image mask[];
30 int x;
31 int y;
32 int frame;
33 int collx,colly,collw,collh;
34
35 public Sprite(byte[] pixels, int pixel_offset, int width, int height, byte[] mask, int mask_offset, int numFrames) {
36 this(
37 com.siemens.mp.ui.Image.createImageFromBitmap(pixels,mask,width,height*numFrames),
38 com.siemens.mp.ui.Image.createImageFromBitmap(mask,width,height*numFrames),
39 numFrames
40 );
41 }
42
43 public Sprite(ExtendedImage pixels, ExtendedImage mask, int numFrames) {
44 this(pixels.getImage(),mask.getImage(),numFrames);
45 }
46
47 public Sprite(Image pixels, Image mask, int numFrames) {
48 this.pixels=new Image[numFrames];
49
50 for (int i=0;i<numFrames;i++) {
51 Image img=Image.createImage(pixels.getWidth(), pixels.getHeight()/numFrames);
52
53 img.getGraphics().drawImage(pixels, 0, -i*pixels.getHeight()/numFrames,0);
54 this.pixels[i]=img;
55 }
56
57 if(mask!=null) {
58 this.mask=new Image[numFrames];
59 for (int i=0;i<numFrames;i++) {
60 Image img=Image.createImage(mask.getWidth(), mask.getHeight()/numFrames);
61
62 img.getGraphics().drawImage(mask, 0, -i*mask.getHeight()/numFrames,0);
63 this.mask[i]=img;
64 }
65 }
66 //this.pixels=pixels;
67 //this.mask=mask;
68 collx=0;
69 colly=0;
70 collw=this.pixels[0].getWidth();
71 collh=this.pixels[0].getHeight();
72 }
73
74 public int getFrame() {
75 //System.out.println("public int getFrame()");
76 return frame;
77 }
78
79 public int getXPosition() {
80 //System.out.println("public int getXPosition()");
81 return x;
82 }
83
84 public int getYPosition() {
85 //System.out.println("public int getYPosition()");
86 return y;
87 }
88
89 public boolean isCollidingWith(Sprite other) {
90 //System.out.println("public boolean isCollidingWith(Sprite other)");
91 return false;
92 }
93
94 public boolean isCollidingWithPos(int xpos, int ypos) {
95 //System.out.println("public boolean isCollidingWithPos(int xpos, int ypos)");
96 return (xpos>=x+collx)&&(xpos<x+collw)&&
97 (ypos>=y+colly)&&(ypos<y+collh);
98 }
99
100 public void setCollisionRectangle(int x, int y, int width, int height) {
101 //System.out.println("public void setCollisionRectangle(int x, int y, int width, int height)");
102 collx=x;
103 colly=y;
104 collw=width;
105 collh=height;
106 }
107
108 public void setFrame(int framenumber) {
109 //System.out.println("public void setFrame(int framenumber)");
110 frame=framenumber;
111 }
112
113 public void setPosition(int x, int y) {
114 //System.out.println("public void setPosition(int x, int y)");
115 this.x=x;
116 this.y=y;
117 }
118
119 protected void paint(Graphics g) {
120 //System.out.println(frame);
121 g.drawImage(pixels[frame], x,y,0);
122 //for(int i=0;i<pixels.length;i++) g.drawImage(pixels[i].getImage(), 20,y*pixels[i].getImage().getHeight(),0);
123 //g.drawImage(mask.getImage(), x,y,0);
124 }
125 }