Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:46 03 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Cmm2 How do I read a binary file to get data?

Author Message
berighteous
Senior Member

Joined: 18/07/2020
Location: United States
Posts: 110
Posted: 09:49pm 08 Aug 2020
Copy link to clipboard 
Print this post

Hi!
I just got my CMM2 a few days ago and I'm having fun playing with it.  I'm starting with porting over a game I did for the pc vga 320x200x256 in 1996.  I have all the artwork in binary data files, each byte a color value 0-ff from the clut for a pixel for a sprite.

For example one of the files starts with 30 animation frames of a 29x27 pixel asteroid, followed by 24 frames of the ship rotating with and without thrusters, etc.  couple hundred images worth of pixel color data.  

I figure I can load the pixel data one by one and pixel them in the correct order on a non visible screen and then cut them and blit them as I need them.  

How do I read in the bytes and convert them to color values to pixel? Converting the data files to text numbers with line feeds to INPUT #1,c say works and I can display the correct colors with pixel j,i,map(c) works, but makes the data file 2-3 x it's original size. And it's extra effort massaging all the data files.

How can I read the hex data directly and use it?  I saw someone mention str2bin but I don't understand how it works.  I need to read the data a byte at a time. (unless there's a better way to do it) can someone explain the process?

Thanks
 
berighteous
Senior Member

Joined: 18/07/2020
Location: United States
Posts: 110
Posted: 09:55pm 08 Aug 2020
Copy link to clipboard 
Print this post



here's what it looks like doing it the hard way.  Here's the code.  The colored line at the bottom is the 8 bit palette.

mode 3,8
'load bmp "bkgr1.bmp"
open "palette.dat" for input as #2
for i = 0 to 255
input #2,r,g,b
map(i)=rgb(r,g,b)
next i
close #2
map set

for i = 0 to 255
pixel i,150,map(i)
next

open "newstuff.dat" for input as #1
for k = 0 to 10
for i = 0 to 26
for j = 0 to 28
input #1,c
pixel 30*k+j,i,map(c)
next j:next i:pause 100 :next k

Edited 2020-08-09 07:56 by berighteous
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 09:58pm 08 Aug 2020
Copy link to clipboard 
Print this post

From the manual
  Quote  INPUT$(nbr, [#]fnbr) Will return a string composed of ‘nbr’ characters read from a file on the SD
card previously opened for INPUT with the file number ‘#fnbr’. This
function will read all characters including carriage return and new line
without translation.


To get the binary values, start by reading the file one 'character' at a time and convert it to binary with

c$ = INPUT$(1,#1)
p = asc(c$)


Jim
VK7JH
MMedit
 
berighteous
Senior Member

Joined: 18/07/2020
Location: United States
Posts: 110
Posted: 10:10pm 08 Aug 2020
Copy link to clipboard 
Print this post

Thanks.  I didn't understand why i couldn't just read in the data as numbers. Reading ascii and converting to numbers seems really weird to me. I knew there'd be a simple solution.  I'm trying to go through the manuals but the old grey matter she ain't what she used to be!

Here's a video of the PC game https://www.youtube.com/watch?v=hJMQlca0DxM
Edited 2020-08-09 08:13 by berighteous
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025