Skip to content

Instantly share code, notes, and snippets.

@Measter
Created February 27, 2015 15:50
Show Gist options
  • Select an option

  • Save Measter/bbb9c5d09a9b2b42f483 to your computer and use it in GitHub Desktop.

Select an option

Save Measter/bbb9c5d09a9b2b42f483 to your computer and use it in GitHub Desktop.
EU4 Revolutionary Flag Generator
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Measter;
namespace ConsoleTestApp
{
internal class EU4RevFlagRenderer
{
private List<SolidBrush> m_flagColors = new List<SolidBrush>();
private Bitmap m_shieldMask, m_shieldFrame;
private Font m_nameFont;
private const int COLUMN_WIDTH = 30;
private const int FLAG_X_OFFSET = 27;
public void Run()
{
GetColors();
m_shieldFrame = new Bitmap( "topbar_big_frame.png" );
m_shieldMask = new Bitmap( "shield_fancy_mask.png" );
m_nameFont = new Font( FontFamily.GenericSansSerif, 8 );
DrawCountryFlags();
}
private void DrawCountryFlags()
{
FileInfo[] files = ( new DirectoryInfo( @"C:\SteamGames\SteamApps\common\Europa Universalis IV\common\countries" ) ).GetFiles( "*.txt" );
foreach( FileInfo country in files )
{
using( StreamReader sr = new StreamReader( country.FullName ) )
{
string line;
while( ( line = sr.ReadLine() ) != null )
{
if( !line.StartsWith( "revolutionary_colors" ) )
continue;
string[] parts = line.Split( '{', '}' )[1].Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );
using( Bitmap flg = DrawColours( int.Parse( parts[0] ), int.Parse( parts[1] ), int.Parse( parts[2] ) ) )
{
AddFrame( flg, country.Name.Split( '.' )[0] );
flg.Save( "flags/" + country.Name.Split( '.' )[0] + ".png", ImageFormat.Png );
}
break;
}
}
}
}
private void AddFrame( Bitmap flg, string name )
{
using( UnsafeBitmap mask = new UnsafeBitmap( m_shieldMask ) )
using( UnsafeBitmap flag = new UnsafeBitmap( flg ) )
{
UnsafeColor background = new UnsafeColor( 255, 35, 35, 35 );
for( int y = 0; y < 181; y++ )
{
for( int x = 0; x < 145; x++ )
{
if( y > 160 )
{
flag.SetPixel( x, y, background );
continue;
}
var maskCol = mask.GetPixel( x, y );
if( maskCol.Red == 0 )
flag.SetPixel( x, y, background );
}
}
}
using( Graphics g = Graphics.FromImage( flg ) )
{
g.DrawImage( m_shieldFrame, 0, 0, 145, 161 );
g.DrawString( name, m_nameFont, m_flagColors[0], 8, 162 );
}
}
private Bitmap DrawColours( int left, int centre, int right )
{
Bitmap b = new Bitmap( 145, 181 );
using( Graphics g = Graphics.FromImage( b ) )
{
g.FillRectangle( m_flagColors[left], FLAG_X_OFFSET, 0, COLUMN_WIDTH, 161 );
g.FillRectangle( m_flagColors[centre], FLAG_X_OFFSET + COLUMN_WIDTH, 0, COLUMN_WIDTH, 161 );
g.FillRectangle( m_flagColors[right], FLAG_X_OFFSET + COLUMN_WIDTH * 2, 0, COLUMN_WIDTH, 161 );
}
return b;
}
private void GetColors()
{
using( StreamReader sr = new StreamReader( @"C:\SteamGames\SteamApps\common\Europa Universalis IV\common\custom_country_colors\00_custom_country_colors.txt" ) )
{
string line;
while( ( line = sr.ReadLine() ) != null )
{
if( !line.StartsWith( "flag_color" ) )
continue;
string[] parts = line.Split( '{', '}' )[1].Split( new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries );
m_flagColors.Add( new SolidBrush( Color.FromArgb( int.Parse( parts[0] ), int.Parse( parts[1] ), int.Parse( parts[2] ) ) ) );
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment