Why is this so slow?

BlitzMax Forums/BlitzMax Programming/Why is this so slow?

Pineapple(Posted 2009) [#1]
I know that the graphics code is decently slow, but it only draws a minimal amount per frame. So why am I never exceeding 5 FPS? This is going to be a CPU-hoarding game (You might be able to tell from the title :P) so I can't waste valuable FPS on something silly like graphics. Also, The speed is so low that most mouse and key strokes aren't even detected.

Download the source and the external data files here: http://www.freewebs.com/grasshopperinteractive/stronghold.rar

I imagine the best way to help would be to use an actual font, but it's imperative that I be able to specify both colors to anything I need, and it has to be size 9 Terminal, which does not seem to be an actual font file, just some teaser that only notepad and the command prompt are allowed to use. In the source I have code that allows the scale of the text to be changed, but that was scrapped shortly ago, so that none of the existing code still uses it.

Thanks in advance


Czar Flavius(Posted 2009) [#2]
The problem seems to be in this section of code
Function DrawChar(x%,y%,f%,scale%=1,invert%=0)

		For Local xxx%=0 To 7
		For Local yyy%=0 To 11
			Local va%=Char[f,xxx,yyy]
			If invert Then va=Abs(va-1)
			If va=1 SetColor cr2,cg2,cb2 Else SetColor cr1,cg1,cb1
			If scale=1
				Plot x+xxx,y+yyy
			Else
				DrawRect x+xxx*scale,y+yyy*scale,scale,scale
			EndIf
		Next
		Next
End Function


From what I can deduce, this function, which can make several plots/rects a call, is called several dozens of times per frame. Plots and drawrects have an overhead, and they're not meant to be called so many times per frame. I believe you can create your own TImages using pixmaps, "in-game", allowing you to draw the different types of grass blades or symbols once to a new image, and then drawing that image instead of drawing the scene on a pixel-by-pixel basis.

The overhead is caused because the CPU can only pass so many instructions to the graphics card per second. It isn't a bottleneck caused by too much strain on either the CPU or graphics card, but the bridge between them. Using images should relieve the problem, as images are stored in the video ram, and a message to draw that image is about the same overhead as a single plot!


Pineapple(Posted 2009) [#3]
How can I, though, change the colors of a pixmap on-the-fly? The number of different colors used for nearly every tile is going to be tremendous. Just see Materials.txt for what my list is of materials so far, and each requires a different color. I suspected that would be a likely cause, but I cannot imagine any other way to affect both the white (which setcolor can change) and the black (which it cannot). For example, in the DrawMap function, gabbro uses all the features I require of the graphics. Setting of foreground (white) and background (black) colors, and the ability to invert them.


Czar Flavius(Posted 2009) [#4]
I'm trying to play your game, to get a better idea of what to suggest, but I can't seem to figure it out. I press D to designate zones, but nothing seems to happen afterwards.


_JIM(Posted 2009) [#5]
Hi, I've tried to run your game as well, but can't figure out what's going on in there, but Czar's idea is a good one.

If you want to generate images with more than one customizable color, then break them apart.

Let's say you have a checker board. Make 2 images: one for the even squares and one for the odd squares. Both will be completely white (RGB: 255, 255, 255). When you draw the checker, you draw the 2 images one after another with "SetColor" before each of them. That way you have a checker board with 2 customizable colors.

You can of course use something different than full white to add detail, and also use more images for more colors.

Since you are already generating tiles (from what I've understood), it should be easy to separate each image into a few "layers".


Pineapple(Posted 2009) [#6]
Alright, that could work. And the game doesn't actually do anything yet (lol) Sorry. It's just a WIP UI.

Also, I found this thread: http://blitzbasic.com/Community/posts.php?topic=67845#879412
And I think that could perhaps be an even more attractive solution.

I need to delete the wxmod folder. I rebuilding modules, and it's been an hour. I don't even use it. It takes so unhumanly long to compile.


Muttley(Posted 2009) [#7]
Dwarf Fortress clone?

As to how to invert the font, just load a pre-inverted copy of the font as well as the normal one and draw that version if you need it inverted.

I'd also look at grouping your updates by colour, that way you're not changing the rendering state so frequently.

So draw all the green grass, all the brown whatever, etc.

Also, looking at your code I'd say avoid pixmaps totally. All you're doing is drawing font chars, so just load the font bitmap up as a animimage with the correct number of frames. Then you can just Drawimage( font, x, y, charIndex)

I hate to say it, but I think before you get much further I'd take a look at your coding style and maybe start using better variable names to make the code more readable. Some comments about what the functions do would be good as well. You may not need them now, but in 6 months time you probably won't know what the hell you were trying to do. ;)


Pineapple(Posted 2009) [#8]
for me, I always use those standard variable names for similar functions. (lol) So the ones that are short may not be very comprehendible be an outsider, but to me, xx, yy, mmbd, p, and l, are just like any other word in the English language. I can easily read code that I wrote years ago, with this same consistent style, without issue.

As for grouping by color, the map is only rendered when the view window is resized, (Ohdang) I just realized I had forgotten to include that part. Oh well.

We'll have to see how much that helps, then probably take some of the advice here, if I don't end up using that colio module.


Pineapple(Posted 2009) [#9]
Alright, _JIM's method is much, much faster. Thanks much!


and conio doesn't seem to work ;(


Jesse(Posted 2009) [#10]
why don't you do it with images instead of pixels? like this:



Pineapple(Posted 2009) [#11]
Thanks to _JIM, I solved the issue with a similar method. Thanks anyway, though.


_JIM(Posted 2009) [#12]
Glad you got it sorted :)