Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Speeding up basic 2D commands. (Box and Dot)

Author
Message
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 20th May 2012 14:55 Edited at: 20th May 2012 14:55
I am making a basic entity editor for a space invader's clone. This editor will allow me to create the ships graphics and properties for the enemy spacecraft and the player controlled friendly air craft (32 X 32 sprites). My issue, it relies heavily on these two commands, BOX and DOT. What can I do to speed those up as those are taking it's toll on the speed of my computer.

Here is how the app. looks with the boxes and dots as of now.



cyril
18
Years of Service
User Offline
Joined: 6th Aug 2007
Location: 7 miles away from big ben
Posted: 20th May 2012 15:26
Have you tried Cloggy's Direct 3D functions plugin?

It performs the same dot and box functions at least 100-200+ times faster than the built-in commands:

http://forum.thegamecreators.com/?m=forum_view&t=69221&b=5
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 20th May 2012 15:35 Edited at: 20th May 2012 16:23
Lemme give them a try, thank you.

**EDIT**

I used this plug in, and it has increased the performance noticeably. Thank you.

***EDIT***

Just an update. I felt like playing around with it and made this. I'll be adding saved color templates for quick access and a alpha bar soon.



Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 20th May 2012 18:20
How about an array that contains color info for each pixel you draw in an entity, then you can make a pick color function (store current color in an array each time you place a pixel) color_array(x,y,3)

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 20th May 2012 18:21 Edited at: 20th May 2012 18:24
That is kinda what I do, but only for parts of it. The image itself, and the templates are in an array, the rest are box and dots. How would I do that for the rest? (I am documenting my code now, and I will post afterwards.)


Here is the undocumented code if you wanna brief through it. I am documenting now.

Scraggle
Moderator
22
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 20th May 2012 19:10
You could make a colour picker tool by putting the small image in a memblock, then getting the position of the mouse nad with a few small calculations extract the colour of the pixel from the memblock - No need to store each pixels colour in an array because it is already in the image.

Ultimately though - Aren't you just re-inventing the wheel? There are plenty of good, free image tools out there that will do this for you, giving you more time to focus on making your game.
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 20th May 2012 19:18 Edited at: 20th May 2012 20:53
The reason for this is a two purpose venture. To get better at programming and I am going to add a couple of features to this that will export the ships is a certain form to directly import into my game, allowing me to easily add a variety of ships easily, higher armored, higher speed, etc. This editor I plan on having settings to export in such a fashion keeping the image and the stats in the same file format, so I only have to deal with one proprietary file versus 2-4 for each different kind of ship.

Hm.. I should look up memblocks again, been forever since I worked with one...

**EDIT**



I just had to post it, I actually like this ship better than the one I made earlier.... My UFO drawing skills are getting better!



This is the commented code. If you have any questions, feel free to ask.

**EDIT**

Ok, with the dot commands, I switched it back to the built in DBPro commands from the d3d commands, and used lock/unlock pixels, and removed a little redundant rendering, and it went from 9-11fps to 30. Here is the update.



Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 21st May 2012 00:17
Colour picker code...



Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st May 2012 05:43
Point is quite a slow command, better to use it sparingly:


I like that you made a bitmap editor just to make a few sprites. That's a coder!

I can think of three cool effect to add:
* Symmetry: painting on one half also paints a mirror image on the other.
* Mirror: takes one half of the image, flips it and pastes on the other.
* Lighting: adjusts the brightness of pixels based on the light position and brightness.

Memblocks would definitely help you here.

WARNING: The above comment may contain sarcasm.
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 21st May 2012 15:50
@Pincho Paxton
Is that to select from the sliders, or to get a color off of the sprite to use it again? If it is the second one, that was the next feature I was going to implement.

@OBese
The mirror feature I was also planning on implementing, but having straight up symmetry would also be a good idea.

The lighting however, that is something I am going to have to work at...

I need to freshen up on memblocks, have not used them in forever...

Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 21st May 2012 18:41 Edited at: 21st May 2012 18:42
Quote: "@Pincho Paxton
Is that to select from the sliders, or to get a color off of the sprite to use it again? If it is the second one, that was the next feature I was going to implement."


It will get any colour on the screen. The colours can be anywhere.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 21st May 2012 19:31
Actually, that code won't get colours from sprites because it uses point which works from the backbuffer, not the frontbuffer:


[b]
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st May 2012 20:50 Edited at: 21st May 2012 21:09
@Maindric
Just started going through your code but thought I'd pull this out
`Clear the screen for a fresh render.
cls

You don't need to comment what commands do, if the reader doesn't know what CLS does then frankly they're not going to understand any of it. It's hard to get out of writing comments like you find in tutorials, but I generally only comment if it's not immediately obvious to me what a piece of code is doing.

`Function that calls a lot of rendering functions, and draws the screen.
renderScreen(colRed, colGreen, colBlue)

This doesn't need commenting either, since it's just a call. If the reader doesn't know what the function does they can go and look at it themselves. If the function needs explaining then it should be down inside the function code.


Noticing a pattern here? 0to9 = (2to11)-2

But we also have a condition for whether shift is pressed

Counting 0to9 twice seems like wasted effort so how can we get them inside the same loop? By turning the nesting inside out! We put the shift key check inside the number key check.


WARNING: The above comment may contain sarcasm.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 21st May 2012 21:46
Quote: "Actually, that code won't get colours from sprites because it uses point which works from the backbuffer, not the frontbuffer:"


It will if you paste them.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 21st May 2012 22:20 Edited at: 21st May 2012 22:51

This is not a good function for two reasons: it has a misleading name "drawResetButton" but it also checks for mouse input. This shouldn't be here, not only because it's unannounced but we don't want mouse input code all over the place, it should be in one place.

I added in the btn function

I saw you were making similar buttons so it's a good idea to have a function to do it for you. We could even adjust this so the program will automatically position the buttons for us.

I changed the sliders to all draw in the same function, plus I drew the gradient bars ahead of time and stored them in an image, that should save some speed as the cpu doesn't need to calculate the gradients just paste the image. This also gives you a good example of a helpful comment.

Reading for the first time you'd wonder what image 1 was and where it came from, and since there is no loaded media it isn't obvious where to look.

[edit]

I have to go now so I'll just post your mutilated code! I broke it (button detection code removed, Colours don't select properly because I combined rgb values) but I will repair and upgrade when I get back!
There's some good stuff in there though so have a look, maybe you can fix it before me!

I remmed out LOCK/UNLOCK PIXELS because I was testing in DBC.
Okay it's REALLY broken in DBP! Strange, I will have to look at it later.

[edit]
Boxes are weird in DBP, I was asking for 0,0,0,5 boxes but it doesn't like it, so I changed to 0,0,1,5 and it works.


WARNING: The above comment may contain sarcasm.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd May 2012 01:27 Edited at: 22nd May 2012 01:28
Quote: "This is not a good function for two reasons: it has a misleading name "drawResetButton" but it also checks for mouse input. This shouldn't be here, not only because it's unannounced but we don't want mouse input code all over the place, it should be in one place."


Perhaps you are right about the name, but I don't see why mouse input should all be in one place... It makes more sense for all the code for a button to be in the same place. That way if you want to change some aspect of the button, you don't have to jump all over the code.

[b]
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd May 2012 02:05
Quote: "It makes more sense for all the code for a button to be in the same place. That way if you want to change some aspect of the button, you don't have to jump all over the code."

You think so? I would use variables for the button's position and size so wouldn't have to jump around to change things. Do you have an example of what you mean? I don't think I'm following you.

WARNING: The above comment may contain sarcasm.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd May 2012 02:57
Quote: "Do you have an example of what you mean? I don't think I'm following you."


Yeah, my entire TopGUI library (there's a link to a video about immediate mode GUIs too)

[b]
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 22nd May 2012 06:07
I haven't been able to work on this at all today, but tomorrow or so I will continue. Diggsey, I think I am going to implement your GUI to this, it seems more flexible then what I was planning, and less work for me.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd May 2012 15:50
@Diggsey
I see why you would want a bounds check for each button if you were doing it modularly like your GUI, but I like to have the button code branch off from the bounds checking because of the fact the mouse can only be over one button at a time, so it seems more efficient to me to do one set of tests to narrow down which button is highlighted. Nice library btw, I might make use of it in future.

WARNING: The above comment may contain sarcasm.
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd May 2012 16:44 Edited at: 22nd May 2012 16:45
Quote: "the mouse can only be over one button at a time, so it seems more efficient to me to do one set of tests to narrow down which button is highlighted."


This seems like premature optimisation to me. Although it might work in a simple situation where you don't have nested controls, the simple situations aren't going to benefit significantly from this optimisation: so much of the screen space isn't taken up by controls, you're going to have to check the mouse against every control anyway 90% of the time.
Also, thanks I hope it's useful

[b]
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 22nd May 2012 21:52
You may have a point there. I stick to a strict input->process->output structure by habit but I can see how that would get disorientating with lots of buttons. Okay you win! I will learn to be more flexible.

WARNING: The above comment may contain sarcasm.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 22nd May 2012 22:21
You can also use Advanced2D, it's a great plugin to help boost 2D things.

TheComet

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2012 00:23
@Maindric
Drawing to an off-screen bitmap then pasting to screen was a great speed saver in DBC, not sure if this technique is negated by un/locking pixels though.

WARNING: The above comment may contain sarcasm.
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 23rd May 2012 03:32 Edited at: 23rd May 2012 03:51
I am going to work on this a little more. I plan on adjusting this around to work with the GUI system that Diggesy made.

@Obese
Yeah, I am going to start converting this for image usage. I think I have a good plan using the get image technique to make most of these to images to begin with.

@TheComet

Yeah, I just discovered that. Hopefully soon, the only draw commands I wil be using soon with be Box and Paste Image.

Let's see what I can come up with.

**EDIT**

Hm... Because the background and the color pickers and the borders never change, I can just draw the entire board, take an image of the screen, and use that for the entirety, and draw the sliders,buttons, and everything that absolutely has to be drawn over the image...

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2012 04:02
yup that will save lots of speed

WARNING: The above comment may contain sarcasm.
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 24th May 2012 17:16
@Diggsey

Hey, how do you get sliders to work? This is what I got:



Of course that's not all the code, but that is the only things that effect the slider. But it doesn't move, any reason?

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th May 2012 18:16
You need to call guiUpdate() just before calling "sync".

[b]
Maindric
16
Years of Service
User Offline
Joined: 22nd Jul 2009
Location:
Posted: 26th May 2012 06:54
Ah.. That makes sense.

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 31st May 2012 00:47
Quote: "`Clear the screen for a fresh render.
cls
You don't need to comment what commands do, if the reader doesn't know what CLS does then frankly they're not going to understand any of it. It's hard to get out of writing comments like you find in tutorials, but I generally only comment if it's not immediately obvious to me what a piece of code is doing.

`Function that calls a lot of rendering functions, and draws the screen.
renderScreen(colRed, colGreen, colBlue)
This doesn't need commenting either, since it's just a call. If the reader doesn't know what the function does they can go and look at it themselves. If the function needs explaining then it should be down inside the function code."


I would agree with you on the first one, but not on the second.
It's always good to error on the side of caution and provide helpful info in comments.

I don't think that his comments were very helpful. I think they mostly need to be written better.

If it were me, I'd have commented both of those but with smaller comments, or grouped related lines under a medium sized comment. Just so the reader knows the general intention that section of the code.

Most people, especially less experienced people have a tendency to not comment code. So it's always better to encourage leaving comments even if they're poorly written. It's better than leaving nothing i think.

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 31st May 2012 19:27
@Mage
Valid points. Subroutines and functions should have self-explanatory names, but yes if it is still unclear what they do they could be commented to help ease of reading. I suppose the test is to read through the main loop as if seeing it for the first time and comment anything that isn't immediately obvious.
"RenderScreen()" is quite obviously a function that renders the screen, this doesn't need commenting. However the actual function does a lot more than just rendering, which isn't obvious from the name or comment.

There are actually tutorials on how to write comments in some books; proper annotation can be extremely beneficial when dealing with anything more than a few dozen lines of code.

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 4th Jun 2012 11:19
Quote: "@Mage
Valid points. Subroutines and functions should have self-explanatory names, but yes if it is still unclear what they do they could be commented to help ease of reading. I suppose the test is to read through the main loop as if seeing it for the first time and comment anything that isn't immediately obvious.
"RenderScreen()" is quite obviously a function that renders the screen, this doesn't need commenting. However the actual function does a lot more than just rendering, which isn't obvious from the name or comment.

There are actually tutorials on how to write comments in some books; proper annotation can be extremely beneficial when dealing with anything more than a few dozen lines of code."


Yeah proper naming conventions and code factoring are equally important.

jobromedia
14
Years of Service
User Offline
Joined: 9th May 2012
Location: Stockholm, Sweden
Posted: 5th Jun 2012 07:40
Nice editor! When I designed my own GUI for my sh*tty MSDOS apps back during the late 90s then I used to define all the objects parameters in custom defined types. I\'ve recreated the types in darkbasic for anyone who wants to use them to build their GUIs with them. Note that you need to design your own functions to draw the GUI though.



MIDI packs for sale.
Best regards
Johan Brodd
DarkDISCUSSION
15
Years of Service
User Offline
Joined: 6th Jul 2011
Location: Ft Madison, IA
Posted: 5th Jun 2012 09:04
Quote: "Speeding up basic 2D commands"




Alas, DarkDISCUSSION tips his hat kindly thanking you for your suggestions and help with a distinct but yet unusual look of appreciation on his pale cold dead face

Login to post a reply

Server time is: 2026-07-08 23:04:09
Your offset time is: 2026-07-08 23:04:09