I am using the COSMOS compiler to write an OS in C# (For those who dont know COSMOS converts IL code into x86 assembly) and I am making a GUI.
I have made GUIs before but now i am trying to make a double buffer.
It sounds rather easy but following is my problem -
I cant use any methods from the System.Drawing Library or any other namespace that uses p/ invokes. Also, I can not use multi Dimensional arrays (I CAN use regular arrays). So my question is how would I implement double buffer?
I know it is possible because I know someone who did it.
Additionally, the only graphical functions I have are SetPixel , GetPixel and Clear. All though I prefer an answer , if any one knows a good article about double buffering ect please tell me.
PS. My OS in 320 x 200 Res LOL
You will need to learn a little bit of DOS C VGA game lore it sounds like ;-)
See Double Buffering, Page Flipping, & Unchained Mode. All the same concepts apply, not sure how they will translate to the C# code though.
Happy coding.
Try to see if you can access System.Drawing.BufferedGraphics. It implements methods for that purpose. Otherwise, I'm afraid you have to do it manually.
Usually, when you draw directly on the screen, the user sees what you’re drawing as it’s being drawn. It’s like when you ask Alex to draw you an apple; you’ll examine him while he draws the apple with the pen. Now if you ask Alex to draw a red apple, then erase it, then draw a blue apple, then erase it then draw a yellow apple, etc… You’ll be looking at him while he’s erasing it each time. Think of it this way: The computer is a fast Alex. So if you ask for a fast animation to be drawn directly on the screen, the user will mark that something is happening between the animation frames: bad flickering!
The solution to flickering is double buffering. A buffer is simply an off-screen area of memory used for drawing. When you use double buffering, instead of drawing directly to the screen, you draw to a back buffer, located in the video memory, and then copy the entire buffer to the screen.
Double buffering can be implemented by using an array to plot to (as opposed to the display) and by blitting the array (when finished plotting) to the display.
Related
I'm working on a tinny software (C# and WPF) that does some filtering to the screen output. Examples on the filtering I'm talking about:
Customizable blue light filtering.
Increase/Decrease screen brightness (via changing the colors, not actual brightness)
Inverting screen colors.
And some more, but the simple idea is to tweak the pixel values of the screen after everything else is done rendering, but before showing the result.
Q1: How can I actually access (and then edit) the screen image/frame?
I've heard of getting and editing the screen DC directly (Win32) but I don't know if it's a good way, nor how to do it exactly.
Q2: what is the best way of applying these filters?
I think that tweaking every single pixel individually is a really bad way of doing it. (My code runs on the CPU and this process needs to be done every frame!)
Q3: Is there an event or hook for when windows is refreshing/updating the screen?
If yes, how can I register this process to it?
Note: I want a really performant way so that I don't lose any frame rate if possible.
Note: My preferred language for this is C#, C++ is ok.
Thanks a ton.
Situation: Simple 3D game project - OpenGL + C#
I read that OpenGL functions doesn't support easily print the text on the screen.
Have anyone clue how to do it? I don't need any too much sophisticated solution.
I just need show for example FPS rate in one corner or show the number of picked up objects in anohter corner.
thx.
One good method for text rendering is to use a texture with the font characters and draw one quad for each character with the good texturing coordinates. This usually gives good results and is platform independant. However this is quite heavy to implement.
Use wgl functions of Opengl32.dll on windows to render text. Example here: http://www.pinvoke.net/default.aspx/opengl32.wglusefontoutlines#
The basic process is you have to build a display list of glyphs in advance (rending the Windows font into an OpenGL context), then you can draw characters on the OpenGL display surface using the characters as indices into the pre-rendered display list.
For a prepackaged managed solution, take a look at Mono's Tao library: http://www.mono-project.com/Tao
http://nehe.gamedev.net/tutorial/freetype_fonts_in_opengl/24001/ This should give you all you want. Its in C++ but I am guessing that should not be a problem. It basically elaborates on what neodelphi suggested.
Although you say you don't need to much complexity and require it for just the FPS, having
a nice font rendering system comes in extremely handy.
HTH
Is WPF be able to manipulate large bitmaps where GDI+ cannot due to memory limitations?
I have bitmaps that are 10,000x10,000 easily, and could even be much larger than that. Worst case I think I can break the single bitmap into large tiles and work with that I guess.
I basically need to do four things
Take an set of tiled images
Put all of those tiles into a single bitmap
Convert the bitmap to black and white
Scan the bitmap looking for changes from black to white
I know how to do these things in GDI+, but the problem I am running into is that the size of my bitmap is too large for the machine I am using, and it causes the program to crash, and I cannot make the image any smaller, so I am hoping that WPF will be able to succeed where GDI+ has failed me.
I don't think WPF will be able to help you here.
Why do you want to use bitmap object? You may as well work with an two-dimensional array of bytes or doubles (or any other type, depending oh what accuracy and range do you need), especially if you work with one channel only. Bitmaps have accessor methods (GetPixel and such) with huge computational overhead, working with arrays is by orders of magnitude faster (I know from personal experience), the only issue is that you can't display them as they are (you would have to convert the array back into image, which is fairly simple). But since you seem to want to do some sort of analysis on the data, I think array would be much more suitable for your needs.
I can post code samples detailing conversion from bitmap (either WPF or WinForms) to array an back if you want.
But remember, that 32bit .NET application can use approximately 1.2-1.4 gigabytes of memory - you have to fit in this space or you start getting OutOfMemory exceptions.
I eventually decided that the best course of action was to only work with the tiles, and then have an array that holds the actual information about each tile that I need. Given the number of tiles, it was the only sensible thing I could do.
Like CommanderZ said. Its Windows PRESENTATION Foundation, not Windows Image-manipulation Foundation.
You should try to either find some kind of image-manipulation library, but looking at size of your image, then doing everything yourself might be only way.
Especialy, you probably wont be able to work with bitmap as a whole, so you are going to work with tiles. Then it becomes problematic if you need to work with neighboring pixels. But I guess you should look into this yourself.
See above.
I need to move my bitmaps around a Form, or perhaps inside a PictureBox in a form. I have not been able to find any tutorials on this specific subject, and even the base GDI+ stuff is a bit confusing. I am looking for a simple and THOROUGHLY explained way on how to do this.
I am needing this for a rendering engine for an 8-bit game I am collaborating on.
GDI+ may not be the best option.
Sprites are generally drawn using "blitting". However, I've read (perhaps outdated?) claims that GDI+ blit operations are slow because they're not hardware accelerated.
If this is 8-bit game rendering, maybe you don't care - but maybe you could use SDL.NET.
In particular, try this tutorial. You should probably follow that first bit of advice (do the hello world first) but this is worth a skim through first to see if it fits.
EDIT - actually, there's not much tutorial there ATM. The main SDL docs (for the C version) are probably OK, but it's still a bit of a pain. Oh well.
I have a .NET GDI+ bitmap object (or if it makes the problem easier a WPF bitmap object) and what I want to to is shift the whole lot by dx,dy (whole pixels) and I would ideally like to do it using .NET but API calls are ok.
It has to be efficient bacause its going to be called 10,000 times say with moderately large bitmaps.
I have implemented a solution using DrawImage - but its slow and it halts the application for minutes while the GC cleans up the temp objects that have been used.
I have also started to work on a version using ScrollDC but so far have had no luck getting it to work on the DC of the bitmap (I can make it work buy creating an API bitmap with bitmap handle, then creating a compatible DC asnd calling ScrollDC but then I have to put it back into the bitmap object).
There has to be an "inplace" way of shifting a bitmap.
mikej
Have you found the Graphics.FromImage method? That will let you manipulate the bitmap directly. I'm not sure what you're using with DrawImage, but Graphics.DrawImage should let you copy an area of the bitmap onto itself (and apply a shift in the process).
Alternatively, given a Graphics object, you can use Graphics.GetHdc, ScrollDC and Graphics.ReleaseHdc.
Do you just want to move the whole graphic? DrawImage has x,y parameters to do that. I'm probably not interpreting your question correctly.
As an aside, GDI is not going to be efficient period when you're working with 10,000 images. If you want anywhere near real-time performance you're going to have to either rework your algorithm or look into a 3D API (like DirectX or OpenGL) or possibly both.
Sorry if my question wasn't too clear.
All I'm trying to do is in place scroll a bitmap i.e. do a shift operation.
For example, a method like Scroll(B,1,0) would shift the entire bitmap B one pixel to the right.
I've got reasonable solution using both DrawImage and the ScrollWindowEx API call which is about 10 time faster.
I'm still trying to work out how I could do something faster using WPF.
I've written up my solution to the problem at Scrolling a bitmap but I'm still not sure that there isn't a better way and if it might be better done in say WPF?