|
Programming Guru
Join Date: Apr 2005
Posts: 1,826
Rep Power: 5 
|
BEHOLD! My sprite Module!
/*
** Sprite2 module
**
** NB: IMPORT VIA " import Sprite2 " AS FIRST LINE.
** MAKE SURE THIS IS IN THE CURRENT WORKING DIRECTORY.
SPRITE MODULE v2.1
Copyright (C) 2005 of JDI by Aaron Voelker
This is an import written in Turing by Aaron Voelker,
for operating sprites without deletion of the background.
By using this code, you are agreeing to all the terms of service
and usage, as follows.
This program is released under the GNU Liscense, so you are
free to use it, but you must give credit by copying the entire
enclosed import (including the liscense and credit to author).
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The author of this program can be contacted for more info at
drsane_@hotmail.com and/or dr.sane@gmail.com
If you have any questions in reference to operating this program
or modifying the contents within, email the given address.
*/
unit
module pervasive Sprite2
export Load, Blit, Erase, Reset, Capture, ScreenDraw, ChangePic,
~. * topLeft, ~. * topRight, ~. * downLeft, ~. * downRight,
~. * center,
~. * maxSprites, ~. * maxSize
% Constants on list of exports
const topLeft := 0
const topRight := 1
const downLeft := 2
const downRight := 3
const center := 4
const maxSprites := 10
const maxSize := 256
% sprite2-used variables
var pi0x, pi0y, spID : int
var Nsprite : array 1 .. maxSprites of
record
picture : int
storage : array 1 .. maxSize of array 1 .. maxSize of int
lx : int
ly : int
sizeX : int
sizeY : int
end record
% sprite ID counter
spID := 0
% Sprite2.Load ( "filename.bmp" )
function Load (filename : string) : int
spID += 1
if spID > maxSprites then
put "Sprites Greater then maxSprites: ", maxSprites
end if
Nsprite (spID).picture := Pic.FileNew (filename)
Nsprite (spID).sizeX := Pic.GetWidth (Nsprite (spID).picture)
Nsprite (spID).sizeY := Pic.GetHeight (Nsprite (spID).picture)
if Nsprite (spID).sizeX > maxSize or Nsprite (spID).sizeY > maxSize
then
put "Image Greater then maxSize: ", maxSize
end if
for x : 0 .. Nsprite (spID).sizeX
for y : 0 .. Nsprite (spID).sizeY
Nsprite (spID).storage (x + 1) (y + 1) := whatdotcolor (x, y)
end for
end for
Nsprite (spID).lx := 0
Nsprite (spID).ly := 0
result spID
end Load
procedure ChangePic (picID : int, filename : string)
Nsprite (picID).picture := Pic.FileNew (filename)
Nsprite (picID).sizeX := Pic.GetWidth (Nsprite (picID).picture)
Nsprite (picID).sizeY := Pic.GetHeight (Nsprite (picID).picture)
if Nsprite (spID).sizeX > maxSize or Nsprite (spID).sizeY > maxSize
then
put "Image Greater then maxSize: ", maxSize
end if
for x : 0 .. Nsprite (spID).sizeX
for y : 0 .. Nsprite (spID).sizeY
Nsprite (spID).storage (x + 1) (y + 1) := whatdotcolor (x, y)
end for
end for
Nsprite (spID).lx := 0
Nsprite (spID).ly := 0
end ChangePic
procedure Capture (picID, tpi0x, tpi0y, drawpos : int)
case drawpos of
label 0 :
pi0x := tpi0x
pi0y := tpi0y - Nsprite (picID).sizeY
label 1 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y - Nsprite (picID).sizeY
label 2 :
pi0x := tpi0x
pi0y := tpi0y
label 3 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y
label 4 :
pi0x := tpi0x - Nsprite (picID).sizeX div 2
pi0y := tpi0y - Nsprite (picID).sizeY div 2
end case
for x : 0 .. Nsprite (picID).sizeX
for y : 0 .. Nsprite (picID).sizeY
Nsprite (picID).storage (x + 1) (y + 1) := whatdotcolor (x +
pi0x, y + pi0y)
end for
end for
Nsprite (picID).lx := pi0x
Nsprite (picID).ly := pi0y
end Capture
procedure Blit (picID, tpi0x, tpi0y, drawtype, drawpos : int)
case drawpos of
label 0 :
pi0x := tpi0x
pi0y := tpi0y - Nsprite (picID).sizeY
label 1 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y - Nsprite (picID).sizeY
label 2 :
pi0x := tpi0x
pi0y := tpi0y
label 3 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y
label 4 :
pi0x := tpi0x - Nsprite (picID).sizeX div 2
pi0y := tpi0y - Nsprite (picID).sizeY div 2
end case
Pic.Draw (Nsprite (picID).picture, pi0x, pi0y, drawtype)
end Blit
% sprite1 := Sprite2.ScreenDraw ( spriteID,
% x, y,
% one of (picCopy, picXor, picMerge, picUnderMerge),
% one of (center, topleft, topright, downleft, downright)
% )
procedure ScreenDraw (picID, tpi0x, tpi0y, drawtype, drawpos : int)
case drawpos of
label 0 :
pi0x := tpi0x
pi0y := tpi0y - Nsprite (picID).sizeY
label 1 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y - Nsprite (picID).sizeY
label 2 :
pi0x := tpi0x
pi0y := tpi0y
label 3 :
pi0x := tpi0x - Nsprite (picID).sizeX
pi0y := tpi0y
label 4 :
pi0x := tpi0x - Nsprite (picID).sizeX div 2
pi0y := tpi0y - Nsprite (picID).sizeY div 2
end case
for x : 0 .. Nsprite (picID).sizeX
for y : 0 .. Nsprite (picID).sizeY
drawdot (Nsprite (picID).lx + x, Nsprite (picID).ly + y,
Nsprite (picID).storage (x + 1) (y + 1))
end for
end for
for x : 0 .. Nsprite (picID).sizeX
for y : 0 .. Nsprite (picID).sizeY
Nsprite (picID).storage (x + 1) (y + 1) := whatdotcolor (x +
pi0x, y + pi0y)
end for
end for
Nsprite (picID).lx := pi0x
Nsprite (picID).ly := pi0y
Pic.Draw (Nsprite (picID).picture, pi0x, pi0y, drawtype)
end ScreenDraw
% Sprite2.Erase ( sprite1 )
procedure Erase (picID : int)
for x : 0 .. Nsprite (picID).sizeX
for y : 0 .. Nsprite (picID).sizeY
drawdot (Nsprite (picID).lx + x, Nsprite (picID).ly + y,
Nsprite (picID).storage (x + 1) (y + 1))
end for
end for
end Erase
% Sprite2.Reset
procedure Reset
spID := 0
end Reset
end Sprite2
And an example of how to use the module:
import Sprite2
setscreen ("graphics:700;700")
setscreen ("nocursor")
setscreen ("noecho")
var x, y, tx, ty, tr1, tr2, tc, sprite1, sprite2 : int
var fixOverlap : boolean
randomize
for i : 1 .. 200
randint (tx, 1, maxx)
randint (ty, 1, maxy)
randint (tr1, - 100, + 100)
randint (tr2, - 100, + 100)
randint (tc, 0, 255)
drawfillbox (tx, ty, tx + tr1, ty + tr2, tc)
end for
sprite1 := Sprite2.Load ("aaron.bmp")
sprite2 := Sprite2.Load ("flag_canada.bmp")
fixOverlap := true
loop
Mouse.Where (x, y, tc)
if x > 100 and y > 100 and x < 600 and y < 600 then
if fixOverlap then
Sprite2.Erase (sprite1)
Sprite2.Erase (sprite2)
Sprite2.Capture (sprite1, x, y, center)
Sprite2.Capture (sprite2, maxx - x, maxy - y, center)
Sprite2.Blit (sprite1, x, y, picCopy, center)
Sprite2.Blit (sprite2, maxx - x, maxy - y, picCopy, center)
else
Sprite2.ScreenDraw (sprite1, x, y, picCopy, center)
Sprite2.ScreenDraw (sprite2, maxx - x, maxy - y, picCopy, center)
end if
end if
if tc = 1 then
Sprite2.Erase (sprite1)
Sprite2.Erase (sprite2)
Sprite2.ChangePic (sprite1, "flag_canada.bmp")
Sprite2.ChangePic (sprite2, "aaron.bmp")
end if
end loop
Sprite2.Erase (sprite1)
Sprite2.Erase (sprite2)
Sprite2.Reset
Hurray for making life easier!
Feel free to use, just follow the instructions. It'll make games in OOT a lot easier to program.
If anyone would like a more indepth explanation on all the procedures and their usage, just ask. It really is worth it.
Last edited by Sane; Oct 30th, 2005 at 9:17 PM.
|