Core2D Graphics Module [widescreen support]
BlitzMax Forums/BlitzMax Programming/Core2D Graphics Module [widescreen support]
| ||
I wrote this while I was deployed. It's part of my Core2D framework. There's cursor, timing, and other neat modules but I'll let you guys check this one out first. If you see anything that can be improved please let me know.
' Core2D Graphics Module
' Cross-Platform Projection Matrix
Strict
Private
Global _width,_height,_depth,_hertz
Global _xOrigin#,_yOrigin#
Global _box1:TBox
Global _box2:TBox
Public
Function Grafx(width,height,depth=0,hertz=60)
_width = width
_height = height
_depth = depth
_hertz = hertz
Select _depth
Case 0
Graphics _width,_height,_depth,_hertz
Case 16,24,32
Local deskRatio# = Float DesktopWidth()/DesktopHeight()
Local gameRatio# = Float _width/_height
Local scale#
Local virtWidth#,virtHeight#
Select True
Case gameRatio < deskRatio 'Wide Screen
scale = Float DesktopHeight() / _height
virtWidth = Float DesktopWidth() / scale
virtHeight = _height
_xOrigin = (virtWidth - _width) / 2.0
_yOrigin = 0
_box1 = TBox.Create(-_xOrigin,0,_xOrigin,_height)
_box2 = TBox.Create(_width,0,_xOrigin,_height)
Case gameRatio > deskRatio 'Tall Screen
scale = Float DesktopWidth() / _width
virtWidth = _width
virtHeight = Float DesktopHeight() / scale
_xOrigin = 0
_yOrigin = (virtHeight - _height) / 2.0
_box1 = TBox.Create(0,-_yOrigin,_width,_yOrigin)
_box2 = TBox.Create(0,_height,_width,_yOrigin)
Default '4:3 Screen
virtWidth = _width
virtHeight = _height
_xOrigin = 0
_yOrigin = 0
End Select
Graphics DesktopWidth(),DesktopHeight(),DesktopDepth(),DesktopHertz()
SetVirtualResolution virtWidth,virtHeight
SetViewport 0,0,virtWidth,virtHeight
SetOrigin _xOrigin,_yOrigin
Default
RuntimeError("Invalid Grafx Mode.")
End Select
End Function
Function GrafxFlip(sync=-1)
SetColor 5,5,10
SetTransform(0,1,1)
If _box1 Then DrawRect(_box1.x,_box1.y,_box1.w,_box1.h)
If _box2 Then DrawRect(_box2.x,_box2.y,_box2.w,_box2.h)
SetColor 255,255,255
Flip sync
End Function
Function GrafxWidth()
Return _width
End Function
Function GrafxHeight()
Return _height
End Function
Function GrafxMouseX()
Return VirtualMouseX() - _xOrigin
End Function
Function GrafxMouseY()
Return VirtualMouseY() - _yOrigin
End Function
Type TBox
Field x#,y#,w#,h#
Function Create:TBox(x#,y#,w#,h#)
Local box:TBox = New TBox
box.x = x
box.y = y
box.w = w
box.h = h
Return box
End Function
End Type
Last edited 2010 |
| ||
| Can someone test this out please. |
| ||
| Looks good to me. You in the service Chroma? |
| ||
| Yep and just got back from Afghanistan a couple months ago. What about you? |
| ||
| Basically this will take your casual game resolution say of 800x600 or 1024x768 and run it in widescreen or tall screen with the appropriate blacked out area. It's cross-platform. Props to sswift for originally inspiring everyone on this board with his version of it. There are 3 modes for setting the graphics. Mode 0 sets it to windowed. Mode 1 sets it to fullscreen. Mode 2 is where the magic happens. It gets the desktop resolution, does a ratio check on it and bumps it against your resolution ratio. Then sets the physical resolution to the desktop resolution and sets the virtual resolution based on what your game resolution is and keeps the perspective correct. If you've got a 1280x1024 monitor it will correct for that too. It does calculate a new Origin X and Y so the upper left corner where the black pillar and the visible part meet is the new 0,0. |
| ||
| Just had a quick go of the demo and it's looking good! Probably not a bug as such (int rounding), but with real desktop at 1440 x 900 and game res set to 1680 x 1050, the lower-right corner is listed as 1678, 1048 rather than 1679, 1049. |
| ||
| Welcome back Chroma! As for me, spent many years as an army brat, never served though. Glad you are safe. |
| ||
| Thanks for testing it James. Yeah that's an issue when the game res is bigger than the desktop res. But the culprit might be the CursorX and CursorY functions. They just return an integer because I figured that much mouse accuracy wasn't needed. This is mainly for putting a 800x600 or 1024x768 game onto a widescreen monitor. Popcap is doing something like this for Plants vs Zombies I believe. Grey Alien might want to check this out, but he may have already coded something like this too. Thanks wmaass. That means a lot to me. |
| ||
| Chroma, compiles fine here. Welcome back - I've got just under a year myself until I draw my gratuity! Right, scrolled to the bottom & changed the value passed to the function - the windowed mode allows you to see the task bar, however the top bit of the screen does not look very windowed. This was on the laptop, will try it in 1920 by 1200 for u later. Seeng the task bar is sweet though with the top bit being dedicated to the app. |
| ||
| Hmm...are you using mode 2? 2 is fullscreen. 0 and 1 really are just windowed and fullscreen (stretched). Mode 2 is what I'm really wondering about how it works on lots of different laptops or machines with widescreen monitors. Basically more and more people have widescreen monitors and I wanted a way for them to be able to play casual games without the stretched graphics. Yep I'm really looking forward to retiring. I'm tired of um...working lol. Yeah that's it in a nutshell. No offense Mark! But when you're passion is programming and that's your job...it's not much of a job lol. EDIT: If you make any changes please let me know as I'm curious if I got any of it wrong. |
| ||
| Chroma: You mentioned PopCap. You might want to look here: http://popcapframework.sourceforge.net There might be some things you could use/port over to your project and the license is very fair. |
| ||
| Had a little mess around on widescreen, works well. Changed the code at the bottom a little, in windowed mode just needed to reduce the rez to lower than the rez of the display you are using to see the windows borders. In mode 2, knocked down the rez a little from 1920x1200 to see if the 'letterbox' area would start to appear - just like on widescreen films, and it did. |
| ||
| I just put this in the code archives. It's been dumbed down a bit. There is just windowed and fullscreen modes and the pillars are only available in fullscreen now. It's a bit more automatic. http://blitzbasic.com/codearcs/codearcs.php?code=2796 |
| ||
| ah, another member of the services ;o) I thought it was only me Chroma, good stuff ;) |
| ||
| This looks good, the only thing I can see that might be an issue is what if the graphics card cant handle "graphics" at the selected desktop resolution? |
| ||
| This is what I mean by "dumbed" down. It uses whatever the desktop resolution is currently set at. |
| ||
| This is very good, but have some issues when you use D3D7Max2DDriver For D3D9Max2DDriver and GLMax2DDriver work perfect. |
| ||
| Thanks for the heads up. I'll check out. Glad you like it. |