CO538 Anonymous Questions and Answers Keyword Index |
This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.
To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.
Keyword reference for occade
2012 |
Submission reference: IN2213
I am trying to use occade in the transterpreter (on Mac), simply trying to add a background:
SHARED OCCADE! occade: INITIAL OCCADE.PARAMS params IS occade.default.params: SEQ params[width] := 640 params[height] := 480 occade.start (occade, params, "Dining Philosophers") occade.load.playfield (occade, "images/bg.png", 0, 0)
That code should work fine, the file does exist. I have also tried bitmap. The error is that the image does not get loaded and it goes into a deadlock. Here is the error that is shown on the terminal:
... [snip screenshot]
Any idea what could be wrong? Thanks.
Your error output refers to rasterio.occ:48. This is what happens when occade (or, rather, the raster library) can't handle the file-type. At a guess, this Transterpreter instance has been compiled without libpng support, so cannot load PNG images. Try converting the file to a PPM instead and see if that works. Other things to check (assuming it's not that) are relative vs. absolute path to the file, display size (image size must match) and depth.
2011 |
Submission reference: IN2083
Hi, this is a follow-up to Question 49 (2011). This is the code that I've managed to get to. It compiles but then I get a runtime error in occade on line 1006 "Mobile type error":
[Snipped]
so that later on I can call:
[Snipped]
I am trying to condense my code into a more elegant solution by making it replicate in this way. However, I'm having issues with the array of OCCADE.SPRITE chans. To compound this, I am unable to find example code of anything that is trying to do something similar – this makes me think my approach is wrong – but I can not see anything way of achieving this functionality.
Did the error message say which file in occade that line was from? Or was it from the code you quoted – in which case, which line was line 1006?
You've worked hard getting this far – and I can see you have found workarounds for at least one restriction in occade concerning file names for icon images (which must be mobile byte arrays!). Please submit what you have, with a cover note on your plan for it and what is and is not working. If you have an ASCII art version, submit that as well, again with a cover note saying you have two versions. Thanks.
Submission reference: IN2082
Hi, this is a follow-up to Question 48 (2011).
Maybe I should be a little more specific. Consider the following;
[snipped code] [n]OCCADE.SPRITE! bundle: SEQ PAR --This could be replicated. occade.start.sprite (occade, bundle[0], -1) bundle[0][req] ! load.image; "someimage.bmp" bundle[0][req] ! move; 20; 10; FALSE bundle[0][req] ! visible; TRUE PAR occade.start.sprite (occade, bundle[1], -1) bundle[1][req] ! load.image; "someimage.bmp" bundle[1][req] ! move; 20; 10; FALSE bundle[1][req] ! visible; TRUE ... [snipped code]
Currently I have it working in a very horrible way involving the following and a lot of not replicated stuff. (it's currently not very dynamic!);
[snip] OCCADE.SPRITE! t0,t1,t2,t3,t4: -- The "Philosophers" sat down OCCADE.SPRITE! node0,node1,node2,node3,node4: -- ... and eating OCCADE.SPRITE! f0,f1,f2,f3,f4: -- Forks when they are at rest OCCADE.SPRITE! l0,l1,l2,l3,l4: -- Forks when they are left forks OCCADE.SPRITE! r0,r1,r2,r3,r4: -- Forks when they are right forks [snip]
The declaration of your bundle array should be:
MOBILE [n]OCCADE.SPRITE! bundle:
The latter declarations should also be arrays (which enables us to replicate code):
MOBILE [n.philosophers]OCCADE.SPRITE! t: -- The "Philosophers" sat down MOBILE [n.philosophers]OCCADE.SPRITE! node: -- ... and eating MOBILE [n.philosophers]OCCADE.SPRITE! f: -- Forks when they are at rest MOBILE [n.philosophers]OCCADE.SPRITE! l: -- Forks when they are left forks MOBILE [n.philosophers]OCCADE.SPRITE! r: -- Forks when they are right forks
But ... you haven't asked a question?
Referrers: Question 50 (2011)
Submission reference: IN2067
Although we were never introduced to occade, do you have any tutorials on making occade work for simple background and sprites? I have looked at the library and have no idea how to use it.
See the "occade" questions listed in the keyword index (linked at the top of each of the Q&A pages). You may find Question 46 (2009) a good place to start. Mail us if you want further help.
Keywords: occade
2009 |
Submission reference: IN1847
I'm having a little difficulty understanding tag load.text in the Occade protocol SPRITE.REQ.
The doc says:
load.text; MOBILE []BYTE; MOBILE []BYTE Set the sprite's image by rendering text. Parameters: MOBILE []BYTE text String to render MOBILE []BYTE font Font image filename
I have no problem sending text to this, but I don't know what to set as the second parameter - this mysterious Font Image filename. Is this something I should be creating, or should there already be some samples available? If it's something I am supposed to be creating, is there any more information on what I should actually put in the file?
I have seen the following line in another question:
sprite[req] ! load.text; buf; "images/font10x20.png"
But this doesn't seem to work in the Transterpreter. I seem to remember reading somewhere that only BMP will work ... in which case the sample code in that question seems a little misleading.
Adam Sampson (ats@kent.ac.uk) writes ...
Font image files are described rather briefly at the top of the Occade documentation page:
"Font image files, used for text rendering, must contain a 16x16 rectangular grid of characters. The font size will be computed automatically from the image size."
There's one (modules/occade/examples/images/font10x20.png) included in the KRoC distribution; it's used in several of the Occade demos. You can download it directly here, along with various other possibly-useful images.
As you say, if you're using the Transterpreter, then you won't be able to load PNG images, but BMP will work; you can use any image editor to convert it, or download a BMP version directly from here.
If you'd like to make your own font, the way I produced the image was to insert the characters I wanted in a terminal window (or text editor), and take a screenshot of it, being careful to crop it to exactly the right size. The characters are in ASCII code order, with 0 top left.
(I think PNG support for the Transterpreter has just become the most-requested feature in the KRoC libraries – we'll fix it!)
Keywords: occade
Submission reference: IN1844
Hello, I'm trying to animate my dining philosophers project with occade, and thus far it's going well (I've got a background showing!). However, I'm very curious as to how the sprites work. My understanding of sprite images are ones where the entire image is broken down into a grid of squares, and as you progress through the lines of squares the image in each changes slightly - and then the program reads these in showing them in rapid sequence, thus appearing to be animated.
I couldn't find any information about this in the occade documentation or on any other occade anon q/a's (there's only 2 =( ). The documentation describes the way to load in the image and show it, but nothing about controlling the animations within the image or what the grid size was in the image or anything, so I hope it would somehow automatically figure that all out itself, but upon trying it, it just plastered the entire image on top of my background where I positioned it.
I was wondering if I could get hold of any examples of sprites files, and the code of them being animated, so I can see how it is done? I can't find any information anywhere else! :(
Cheers.
Adam Sampson (ats@kent.ac.uk) writes ...
Your understanding of sprite images is not right – a sprite is just a moveable image that can be displayed against a fixed background. Older machines had dedicated hardware to draw sprites; these days we do the same thing in software. You can use sprites to implement flipbook animation as you describe, but there's nothing that says sprites have to be animated, nor that the images have to be contiguous in memory.
There's a good Wikipedia article on sprites and what sorts of hardware support different machines had for them.
The machine I had in mind when designing Occade originally was the Atari 2600, which had hardware support for drawing sprites. On the 2600, if you wanted to animate a sprite in your game, you'd just upload each new frame of the animation to the graphics chip as the appropriate time was reached. To do animation in Occade, you can use exactly the same approach -- whenever you want to move to a new frame of animation, send the sprite a load request with the new frame you want it to display.
The easiest way is to save each frame of animation to a separate file. In the Parrot Attack example game, there are four frames of animation for the parrots (two facing left, and two facing right), and PROC alien calls PROC set.alien.shape to change to the next frame each time a timer expires; this just uses a CASE statement to pick which of several images to load.
But let's suppose you've drawn an animation with lots of frames, so you want to put them all in one big file, like you suggested. To do this, you need to use occam-pi's RASTER data type, which represents an image; the standard library rasterio.module, which lets you load images from files into RASTERs; and the sprite's load.raster request, which makes it set its image from a RASTER. Since RASTERs are really just (mobile) arrays of pixels, you can copy data around between them however you like -- so you can copy the frame of animation you want out of the big image into a frame-sized RASTER, and then use that frame to set the sprite's image.
Sound complicated? Well, sort of, but the code's not too bad -- although it needs to use some occam-pi facilities (notably mobile arrays) that you probably haven't seen yet in CO538. Here's a working example, that expects a 320x32 image called "images/numbers.png" containing ten 32x32 frames arranged in a horizontal row:
#INCLUDE "occade.module" #INCLUDE "rasterio.module" -- The size of each frame. VAL INT FRAME.WIDTH IS 32: VAL INT FRAME.HEIGHT IS 32: PROC my.sprite (SHARED OCCADE! occade) OCCADE.SPRITE! sprite: RASTER image, frame: INT num.frames: SEQ -- Start the sprite. occade.start.sprite (occade, sprite, -1) -- Load the image into a RASTER. read.raster ("images/numbers.png", image) -- Work out how many frames we loaded. num.frames := (SIZE image[0]) / FRAME.WIDTH -- Create a new frame-sized RASTER. (A RASTER is really just a mobile 2D -- array, with the outer dimension being the Y axis.) frame := MOBILE [FRAME.HEIGHT][FRAME.WIDTH]INT INITIAL INT frame.num IS 0: WHILE TRUE SEQ -- Copy the appropriate frame out of the loaded image. VAL INT x.offset IS FRAME.WIDTH * frame.num: SEQ y = 0 FOR FRAME.HEIGHT SEQ x = 0 FOR FRAME.WIDTH frame[y][x] := image[y][x.offset + x] -- Set the sprite's image from the frame. sprite[req] ! load.raster; frame sprite[resp] ? CASE return.raster; frame -- Put the sprite in the middle of the screen and make it visible. sprite[req] ! move; 320; 240; TRUE sprite[req] ! visible; TRUE -- Move to the next frame number (wrapping round at num.frames). frame.num := (frame.num + 1) \ num.frames -- Wait for a bit. occade.delay (100000) : PROC main (CHAN BYTE keys?, scr!, err!) SHARED OCCADE! occade: INITIAL OCCADE.PARAMS params IS occade.default.params: SEQ params[width] := 640 params[height] := 480 occade.start (occade, params, "Flipbook animation demo") my.sprite (occade) :
This will not be in the exam!
It's only fair to warn you that there are some caveats with using rasterio.module instead of getting Occade to load your images for you: rasterio doesn't support BMP format images, only PNG, and it doesn't work on the Transterpreter at the moment.
The source code for Parrot Attack and the other examples, along with some example images including the one for the code above, are available online here.
Keywords: occade
Referrers: Question 35 (2011)
Submission reference: IN1834
I am trying to get random text to scroll in occade. My current method involves creating a sprite and then loading text using a font/image package.
However I want to change this text often. So far I have this methodology:
Before I try and bodge a way of doing this, is there an easy/ier way of doing this?
Also does occam provide a way of pushing a value to an array? Thanks.
Adam Sampson (ats@kent.ac.uk) writes ...
Sounds like you have it pretty much figured out already. :)
You don't need to create a new sprite each time -- you can just create one sprite, make it visible, then send it the "load.text" message each time you want to change the message it's displaying. The score displays in Parrot Attack work this way, for example.
To insert a character at the start of an array, the easiest way is just to shift everything else up by one -- copy the second-last character over the last, then the third-last over the second-last, and so on. You can do this using a replicated SEQ with the STEP keyword to make it count backwards.
Here's what that looks like as an Occade program:
#INCLUDE "occade.module" PROC demo.text.maker (CHAN BYTE c!) WHILE TRUE SEQ ch = 'a' FOR 26 SEQ c ! BYTE ch -- the replicator control variable is an INT. occade.delay (100000) -- set whatever speed your display can take. : PROC display.text (SHARED OCCADE! occade, CHAN BYTE c?) VAL INT LEN IS 20: INITIAL [LEN]BYTE buf IS [i = 0 FOR LEN | ' ']: -- we've not told you about the "array comprehension" syntax above ... -- it just constructs an array of size LEN, filled with spaces. OCCADE.SPRITE! sprite: SEQ occade.start.sprite (occade, sprite, -1) sprite[req] ! load.text; buf; "images/font10x20.png" sprite[req] ! move; 320; 240; TRUE sprite[req] ! visible; TRUE WHILE TRUE BYTE ch: SEQ -- wait for new byte char to arrive on channel c ? ch -- add this new byte char to the start of my array SEQ i = LEN - 1 FOR LEN - 1 STEP -1 buf[i] := buf[i - 1] buf[0] := ch -- update the sprite sprite[req] ! load.text; buf; "images/font10x20.png" : PROC main (CHAN BYTE kbd?, scr!, err!) SHARED OCCADE! occade: INITIAL OCCADE.PARAMS params IS occade.default.params: SEQ params[width] := 640 params[height] := 480 occade.start (occade, params, "Scrolly text demo") CHAN BYTE c: PAR demo.text.maker (c!) display.text (occade, c?) :
Submission reference: IN1829
When attempting to assign a background in occade using:
occade.load.playfield (occade, "images/background.png", 0, 0)
I get the following error:
STOP error The error occured on or around line 41 in rasterio.occ
The occade window loads correctly prior to that statement being made.
Adam Sampson (ats@kent.ac.uk) writes ...
To load PNG images, you need a KRoC that's been built with support for the libpng library. (You can't do this for the Transterpreter because it relies on CIF – although I think Carl had a fix for that in the LLVM branch? If you don't know about CIF and LLVM and are curious, come and ask us, :)
But if you have occade working, a quick workaround would be to use images in BMP format instead; SDL has built-in support for loading BMP files, so occade can always do that.
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |