Images in Pascal??
(Sorry about my last post... the name screwed up somehow...)
How do you load image files in Pascal/Delphi???
How do you embed music files?????
please anyone... help me!
Depends, if you wanna do it in standard GDI you can use some API calls. If you want DirectX I guess you have to write your own Bitmap loader. And sounds can be played with the function sndPlaySoundA from "winmm.dll". Check out the Delphi help files on loading bitmaps. And BTW, you can edit your topic title by editing your initial post.
Sand Hawk
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
Sand Hawk
----------------
-Earth is 98% full. Please delete anybody you can.
My Site
Yep, it depends on what API you''re using - GDI, the VCL, DirectDraw, OpenGL, SDL...
If you''re using the VCL then you can simply dump a TImage (in the Additional tab) onto your form and load its picture by clicking on the ellipses (three dots) beside the picture property.
If you want to load it at run-time then you can declare a TBitmap, create it and load your picture in (remember to free it at some point too). This is simpler than it sounds:
It''s usually a good idea to use try..finally blocks. These can be used to safeguard resources (like objects you create, files you open, etc.) so that they are always deallocated - in this case, you want to make sure that if the bitmap is created it will always be freed, regardless of any errors:
The above, if it successfully creates the bitmap, guarantees that the memory allocated will be freed. This protects you from resource leaks.
That example code is a bit inefficient if you plan to use the bitmap a lot (for example, for animations). The usual course of action is to declare your bitmap as a member of the form (inside the private section), create and load it in OnCreate and free it in the OnDestroy event.
---
You can embed sound files by creating resource files and linking them to your exe. You create a .RC file containing instructions, such as:
my_sound_name WAVE "moo.wav"
Then, you run "brcc32 filename" from the command prompt (for example, if the above .rc was someres.rc, you''d say "brcc32 someres"). This gives you a .RES file with the same name as your .rc script.
You can include the resource by doing "{$R someres.res}". A good place to add this is just after the other {$R *.dfm} (which includes the resource file for your Delphi form, generated automatically in the IDE.
You can play the sound using the PlaySound function. Add "mmsystem" to the uses clause at the top, alongside the other stuff (e.g. "windows, sysutils, classes", etc.).
The function PlaySound has a flag SND_RESOURCE that allows you to play a file from a resource. Example code:
PlaySound(''my_sound_name'', hInstance, SND_ASYNC or SND_RESOURCE or SND_LOOP);
A few notes: the first parameter is the name of the sound as specified in the first part of the .rc script. If you want to pass the name as a string then you have to typecast it as a PChar, as that''s what the function needs:
The second parameter is the current instance of the program. This is one of those low-level details. Delphi provides this from the system unit, and calls it "hInstance". You can simply bung that in there.
The third parameter is a list of flags. You''ll see from the help on PlaySound that there are lots of them. You can combine them using the ''or'' operator (don''t worry too much about this - it combines them bit-by-bit, but just think of them as joining the flags. You don''t need to care about the details).
Some useful flags:
SND_ASYNC - start the sound immediately
SND_LOOP - loop the sound (good for music, I guess)
SND_RESOURCE - load from a resource file
SND_FILENAME - the opposite of SND_RESOURCE, play it from an external file (not a resource)
SND_NODEFAULT - stops the ''chime'' if the file isn''t found
You can stop any playing sound using
PlaySound(nil, hInstance, SND_PURGE);
Note that any files in a resource file get directly linked into your exe . This means that you don''t have to distribute them. However, the down side is that your exe size will grow substantially.
resourcesound.zip (17K)
You might also want to have a look at some excellent sound libraries:
FMod
Bass
Both those come with a myriad of cool options, such as 3D sound, playing MP3s, etc.
If you''re using the VCL then you can simply dump a TImage (in the Additional tab) onto your form and load its picture by clicking on the ellipses (three dots) beside the picture property.
If you want to load it at run-time then you can declare a TBitmap, create it and load your picture in (remember to free it at some point too). This is simpler than it sounds:
procedure TForm1.ShowBitmap(const Filename: String);var Bmp: TBitmap;begin if not FileExists(Filename) then Exit; Bmp := TBitmap.Create; Bmp.LoadFromFile(Filename); Canvas.Draw(0,0, Bmp); // draw the bmp to screen Bmp.Free; // free anything you createend;
It''s usually a good idea to use try..finally blocks. These can be used to safeguard resources (like objects you create, files you open, etc.) so that they are always deallocated - in this case, you want to make sure that if the bitmap is created it will always be freed, regardless of any errors:
procedure TForm1.ShowBitmap(const Filename: String);var Bmp: TBitmap;begin if not FileExists(Filename) then Exit; Bmp := TBitmap.Create; try Bmp.LoadFromFile(Filename); Canvas.Draw(0,0, Bmp); finally Bmp.Free; end;end;
The above, if it successfully creates the bitmap, guarantees that the memory allocated will be freed. This protects you from resource leaks.
That example code is a bit inefficient if you plan to use the bitmap a lot (for example, for animations). The usual course of action is to declare your bitmap as a member of the form (inside the private section), create and load it in OnCreate and free it in the OnDestroy event.
---
You can embed sound files by creating resource files and linking them to your exe. You create a .RC file containing instructions, such as:
my_sound_name WAVE "moo.wav"
Then, you run "brcc32 filename" from the command prompt (for example, if the above .rc was someres.rc, you''d say "brcc32 someres"). This gives you a .RES file with the same name as your .rc script.
You can include the resource by doing "{$R someres.res}". A good place to add this is just after the other {$R *.dfm} (which includes the resource file for your Delphi form, generated automatically in the IDE.
You can play the sound using the PlaySound function. Add "mmsystem" to the uses clause at the top, alongside the other stuff (e.g. "windows, sysutils, classes", etc.).
The function PlaySound has a flag SND_RESOURCE that allows you to play a file from a resource. Example code:
PlaySound(''my_sound_name'', hInstance, SND_ASYNC or SND_RESOURCE or SND_LOOP);
A few notes: the first parameter is the name of the sound as specified in the first part of the .rc script. If you want to pass the name as a string then you have to typecast it as a PChar, as that''s what the function needs:
procedure TForm1.DoSomething(const SoundName: String);begin PlaySound(PChar(SoundName), ...end;
The second parameter is the current instance of the program. This is one of those low-level details. Delphi provides this from the system unit, and calls it "hInstance". You can simply bung that in there.
The third parameter is a list of flags. You''ll see from the help on PlaySound that there are lots of them. You can combine them using the ''or'' operator (don''t worry too much about this - it combines them bit-by-bit, but just think of them as joining the flags. You don''t need to care about the details).
Some useful flags:
SND_ASYNC - start the sound immediately
SND_LOOP - loop the sound (good for music, I guess)
SND_RESOURCE - load from a resource file
SND_FILENAME - the opposite of SND_RESOURCE, play it from an external file (not a resource)
SND_NODEFAULT - stops the ''chime'' if the file isn''t found
You can stop any playing sound using
PlaySound(nil, hInstance, SND_PURGE);
Note that any files in a resource file get directly linked into your exe . This means that you don''t have to distribute them. However, the down side is that your exe size will grow substantially.
resourcesound.zip (17K)
You might also want to have a look at some excellent sound libraries:
FMod
Bass
Both those come with a myriad of cool options, such as 3D sound, playing MP3s, etc.
Thanks for the advise, but I understand a very small amonunt of what you just said, alimonster, and even less what you said Sand_Hawk. I haven''t got to figure out how to do much, because i started learning oascal (the DOS stuff like readln()), and I''m finding it impossable to move from the basics (DOS pascal, Terminal programs,) to windows Delphi OOP. There are Just TOO MANY GODDAMN functions that I need to learn.
I am in way over my head, and I need help. I will continue to post things in these and other threads (Only my own right now, ''cause I don''t got enough experiance to help other people, that is unless they''re pittifully horable at programing).
And, also, does either of you know where I could get free custom graphics? I am REALY bad at making graphics!
Once again, thanks both of you, please continue visiting my posts, and help keep them alive. Not to mention helping me have more friends (everyone says I need more friends, what''s wrong with one or two?).
P.S. I have NO experiance with DirectX do either of you know ether? I WAS hoping on using DirectX, but I need to learn the basics first... I think. (Not MORE basics... [exhasperated cry]).
I am in way over my head, and I need help. I will continue to post things in these and other threads (Only my own right now, ''cause I don''t got enough experiance to help other people, that is unless they''re pittifully horable at programing).
And, also, does either of you know where I could get free custom graphics? I am REALY bad at making graphics!
Once again, thanks both of you, please continue visiting my posts, and help keep them alive. Not to mention helping me have more friends (everyone says I need more friends, what''s wrong with one or two?).
P.S. I have NO experiance with DirectX do either of you know ether? I WAS hoping on using DirectX, but I need to learn the basics first... I think. (Not MORE basics... [exhasperated cry]).
Don't worry - we're here to help you. I'll throw you some helpful links:
Essential Delphi by Marco Cantu
Essential Pascal by Marco Cantu
DelphiLand (IIRC, only a few of the tutorials are free - but it'll still be useful)
Delph.about.com - a sprawling mass of links and tutorials. Be sure to have a wade through sometime, including the beginner's tutorials
Frank's Delphi Lessons
More tutorials
Btw, I know a bit about DirectX - I've used DirectDraw, DirectSound/Music and DirectInput. However, I'd recommend you have a play around with PowerDraw or DelphiX/UnDelphiX (find the links fromTurbo's sidebar). These are nice wrappers around DirectX that take care of most of the hard stuff. There are a couple of tutorials by Dominique Louis over [1], [2] and [3].
You could also have a look at SDL, which is cross-platform: over here.
EDIT: oh, about the graphics thing - ever heard of "programmer's art"?
[edited by - Alimonster on August 12, 2002 9:37:08 AM]
Essential Delphi by Marco Cantu
Essential Pascal by Marco Cantu
DelphiLand (IIRC, only a few of the tutorials are free - but it'll still be useful)
Delph.about.com - a sprawling mass of links and tutorials. Be sure to have a wade through sometime, including the beginner's tutorials
Frank's Delphi Lessons
More tutorials
Btw, I know a bit about DirectX - I've used DirectDraw, DirectSound/Music and DirectInput. However, I'd recommend you have a play around with PowerDraw or DelphiX/UnDelphiX (find the links fromTurbo's sidebar). These are nice wrappers around DirectX that take care of most of the hard stuff. There are a couple of tutorials by Dominique Louis over [1], [2] and [3].
You could also have a look at SDL, which is cross-platform: over here.
EDIT: oh, about the graphics thing - ever heard of "programmer's art"?
[edited by - Alimonster on August 12, 2002 9:37:08 AM]
do either of you know what the following error means?:
[error] start.dpr(21):Missing operator or semicolon
my code is(partially created by Delphi):
program start;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
Charactor = record
Name, Password, Charclass, Race: string;
items: array[1..4, 1..5] of string;
Spells: array[1..5, 1..10] of string;
Height, Weight: integer;
end;
var
Name, Password, Charclass, Race: string;
begin
writeln(''By what name will you be known: '')
readln•(name);
if name = ''bobbias'' then
writeln(''do you know the password?(Y, N) '');
read(password);
end.
•the error is highlighted the line and placed the curser on the star(•).
please if you know what is wrong, please, tell me!
P.S. this is a small part of a text based game. called Dragon Quest(ORIGINAL NAME ) I need to do alot of work yet, but this is the bare bones of the name and password piece(if you know how to implement somthng to make this work properly please tell me) my goal is to make a database-like file that stores the names of existing charactors (and the stats ie. strength, stamina...) but I don''t know how to do that. I f you can give some advise, please, I''ve been trying to get it to compile forever.
[error] start.dpr(21):Missing operator or semicolon
my code is(partially created by Delphi):
program start;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
Charactor = record
Name, Password, Charclass, Race: string;
items: array[1..4, 1..5] of string;
Spells: array[1..5, 1..10] of string;
Height, Weight: integer;
end;
var
Name, Password, Charclass, Race: string;
begin
writeln(''By what name will you be known: '')
readln•(name);
if name = ''bobbias'' then
writeln(''do you know the password?(Y, N) '');
read(password);
end.
•the error is highlighted the line and placed the curser on the star(•).
please if you know what is wrong, please, tell me!
P.S. this is a small part of a text based game. called Dragon Quest(ORIGINAL NAME ) I need to do alot of work yet, but this is the bare bones of the name and password piece(if you know how to implement somthng to make this work properly please tell me) my goal is to make a database-like file that stores the names of existing charactors (and the stats ie. strength, stamina...) but I don''t know how to do that. I f you can give some advise, please, I''ve been trying to get it to compile forever.
Hey! I believe u r missing a semicolon after the writeln line
Spartacus
program start;{$APPTYPE CONSOLE}usesSysUtils;typeCharactor = recordName, Password, Charclass, Race: string;items: array[1..4, 1..5] of string;Spells: array[1..5, 1..10] of string;Height, Weight: integer;end;varName, Password, Charclass, Race: string;beginwriteln(''By what name will you be known: '') {<-- no semicolon here, there should be one }readln•(name);if name = ''bobbias'' thenwriteln(''do you know the password?(Y, N) '');read(password);end.
Spartacus
Real programmers don''t document, if it was hard to write it should be hard to understand
Real programmers don't document, if it was hard to write it should be hard to understand
stick a semicolon ( at the end of the writeln(...) line above the readln(...) which is failing.
e.g.
writeln(''By what name will you be known: '')
should be
writeln(''By what name will you be known: '');
e.g.
writeln(''By what name will you be known: '')
should be
writeln(''By what name will you be known: '');
thanks for the help. I''m so surprised I missed that .
But at least I can keep working on the Dragon Quest project.
anyone who helps me will be considered a friend. So if anyone wants to reply, they can do so without me dissing them if thier code dosn''t work or for other reasons.
But at least I can keep working on the Dragon Quest project.
anyone who helps me will be considered a friend. So if anyone wants to reply, they can do so without me dissing them if thier code dosn''t work or for other reasons.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement