Skip to content

Commit

Permalink
Merge remote branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Walzer committed Jun 23, 2011
2 parents 70c0024 + 83b6430 commit c1d985c
Show file tree
Hide file tree
Showing 13 changed files with 283 additions and 29 deletions.
23 changes: 19 additions & 4 deletions HelloLua/android/src/org/cocos2dx/hellolua/HelloLua.java
@@ -1,11 +1,11 @@
package org.cocos2dx.hellolua;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.hellolua.R;


import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.KeyEvent;

public class HelloLua extends Cocos2dxActivity{
protected void onCreate(Bundle savedInstanceState){
Expand All @@ -15,8 +15,8 @@ protected void onCreate(Bundle savedInstanceState){
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);

setContentView(R.layout.game_demo);
mGLView = (Cocos2dxGLSurfaceView) findViewById(R.id.game_gl_surfaceview);
mGLView = new LuaGLSurfaceView(this);
setContentView(mGLView);

// Get the size of the mGLView after the layout happens
mGLView.post(new Runnable() {
Expand Down Expand Up @@ -47,3 +47,18 @@ protected void onResume() {
System.loadLibrary("game");
}
}

class LuaGLSurfaceView extends Cocos2dxGLSurfaceView{

public LuaGLSurfaceView(Context context){
super(context);
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
// exit program when key back is entered
if (keyCode == KeyEvent.KEYCODE_BACK) {
android.os.Process.killProcess(android.os.Process.myPid());
}
return super.onKeyDown(keyCode, event);
}
}
9 changes: 9 additions & 0 deletions HelloLua/android/src/org/cocos2dx/lib/Cocos2dxActivity.java
Expand Up @@ -45,6 +45,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
Expand All @@ -58,6 +59,7 @@
public class Cocos2dxActivity extends Activity{
public static int screenWidth;
public static int screenHeight;
public static Context context;
private static Cocos2dxMusic backgroundMusicPlayer;
private static Cocos2dxSound soundPlayer;
private static Cocos2dxAccelerometer accelerometer;
Expand All @@ -72,6 +74,8 @@ public class Cocos2dxActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

context = this;

// get frame size
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Expand All @@ -94,6 +98,11 @@ public void handleMessage(Message msg){
};
}

public static String getCurrentLanguage() {
String languageName = java.util.Locale.getDefault().getLanguage();
return languageName;
}

public static void showMessageBox(String title, String message){
Message msg = new Message();
msg.what = HANDLER_SHOW_DIALOG;
Expand Down
81 changes: 75 additions & 6 deletions HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java
Expand Up @@ -8,22 +8,91 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.view.Gravity;
import android.widget.TextView;

public class Cocos2dxBitmap{
public static void createTextBitmap(String content, int fontSize){
/*
* The values are the same as cocos2dx/platform/CCImage.h.
* I think three alignments are ok.
*/
private static final int ALIGNCENTER = 0x33;
private static final int ALIGNLEFT = 0x31;
private static final int ALIGNRIGHT = 0x32;

public static void createTextBitmap(String content, int fontSize, int alignment){
TextProperty textProperty = getTextWidthAndHeight(content, fontSize);

// create TextView and set corresponding property
TextView tv = new TextView(Cocos2dxActivity.context);
tv.setText(content);
tv.measure(textProperty.maxWidth, textProperty.height);
tv.setTextSize(fontSize);
tv.layout(0, 0, textProperty.maxWidth, textProperty.height);
setTextViewAlignment(tv, alignment);

// draw text to bitmap
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tv.draw(canvas);

initNativeObject(bitmap);
}

private static void setTextViewAlignment(TextView tv, int alignment){
switch (alignment){
case ALIGNCENTER:
tv.setGravity(Gravity.CENTER);
break;

case ALIGNLEFT:
tv.setGravity(Gravity.LEFT);
break;

case ALIGNRIGHT:
tv.setGravity(Gravity.RIGHT);
break;

default:
tv.setGravity(Gravity.CENTER);
break;
}
}

private static class TextProperty{
int maxWidth;
int height;

TextProperty(int w, int h){
this.maxWidth = w;
this.height = h;
}
}

private static TextProperty getTextWidthAndHeight(String content, int fontSize){
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(fontSize);

FontMetricsInt fm = paint.getFontMetricsInt();
int h = (int)Math.ceil(fm.descent - fm.ascent) + 2;
int w = (int)Math.ceil(paint.measureText(content, 0, content.length()));

Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(content, 0, h, paint);
String[] lines = content.split("\\n");

initNativeObject(bitmap);
/*
* Compute the max width
*/
int w = 0;
int temp = 0;
for (String line : lines){
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
if (temp > w){
w = temp;
}
}

return new TextProperty(w, h * lines.length);
}

private static void initNativeObject(Bitmap bitmap){
Expand Down
Expand Up @@ -195,6 +195,7 @@ public void setTextField(TextView view) {
linearParams.height = 0;
mTextField.setLayoutParams(linearParams);
mTextField.setOnEditorActionListener(textInputWraper);
this.requestFocus();
}
}

Expand Down
4 changes: 4 additions & 0 deletions HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxActivity.java
Expand Up @@ -45,6 +45,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
Expand All @@ -58,6 +59,7 @@
public class Cocos2dxActivity extends Activity{
public static int screenWidth;
public static int screenHeight;
public static Context context;
private static Cocos2dxMusic backgroundMusicPlayer;
private static Cocos2dxSound soundPlayer;
private static Cocos2dxAccelerometer accelerometer;
Expand All @@ -72,6 +74,8 @@ public class Cocos2dxActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

context = this;

// get frame size
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Expand Down
81 changes: 75 additions & 6 deletions HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java
Expand Up @@ -8,22 +8,91 @@
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetricsInt;
import android.view.Gravity;
import android.widget.TextView;

public class Cocos2dxBitmap{
public static void createTextBitmap(String content, int fontSize){
/*
* The values are the same as cocos2dx/platform/CCImage.h.
* I think three alignments are ok.
*/
private static final int ALIGNCENTER = 0x33;
private static final int ALIGNLEFT = 0x31;
private static final int ALIGNRIGHT = 0x32;

public static void createTextBitmap(String content, int fontSize, int alignment){
TextProperty textProperty = getTextWidthAndHeight(content, fontSize);

// create TextView and set corresponding property
TextView tv = new TextView(Cocos2dxActivity.context);
tv.setText(content);
tv.measure(textProperty.maxWidth, textProperty.height);
tv.setTextSize(fontSize);
tv.layout(0, 0, textProperty.maxWidth, textProperty.height);
setTextViewAlignment(tv, alignment);

// draw text to bitmap
Bitmap bitmap = Bitmap.createBitmap(textProperty.maxWidth, textProperty.height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tv.draw(canvas);

initNativeObject(bitmap);
}

private static void setTextViewAlignment(TextView tv, int alignment){
switch (alignment){
case ALIGNCENTER:
tv.setGravity(Gravity.CENTER);
break;

case ALIGNLEFT:
tv.setGravity(Gravity.LEFT);
break;

case ALIGNRIGHT:
tv.setGravity(Gravity.RIGHT);
break;

default:
tv.setGravity(Gravity.CENTER);
break;
}
}

private static class TextProperty{
int maxWidth;
int height;

TextProperty(int w, int h){
this.maxWidth = w;
this.height = h;
}
}

private static TextProperty getTextWidthAndHeight(String content, int fontSize){
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(fontSize);

FontMetricsInt fm = paint.getFontMetricsInt();
int h = (int)Math.ceil(fm.descent - fm.ascent) + 2;
int w = (int)Math.ceil(paint.measureText(content, 0, content.length()));

Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(content, 0, h, paint);
String[] lines = content.split("\\n");

initNativeObject(bitmap);
/*
* Compute the max width
*/
int w = 0;
int temp = 0;
for (String line : lines){
temp = (int)Math.ceil(paint.measureText(line, 0, line.length()));
if (temp > w){
w = temp;
}
}

return new TextProperty(w, h * lines.length);
}

private static void initNativeObject(Bitmap bitmap){
Expand Down
5 changes: 1 addition & 4 deletions cocos2d-win32.vc2008.sln
Expand Up @@ -28,10 +28,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "HelloLua", "HelloLua\win32\
{98A51BA8-FC3A-415B-AC8F-8C7BD464E93E} = {98A51BA8-FC3A-415B-AC8F-8C7BD464E93E}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblua", "lua\liblua.vcproj", "{DDC3E27F-004D-4DD4-9DD3-931A013D2159}"
ProjectSection(ProjectDependencies) = postProject
{98A51BA8-FC3A-415B-AC8F-8C7BD464E93E} = {98A51BA8-FC3A-415B-AC8F-8C7BD464E93E}
EndProjectSection
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "liblua", "lua\proj.win32\liblua.vcproj", "{DDC3E27F-004D-4DD4-9DD3-931A013D2159}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
4 changes: 2 additions & 2 deletions cocos2dx/platform/android/CCImage_android.cpp
Expand Up @@ -73,7 +73,7 @@ class BitmapDC
}

// get method of createBitmap
jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;I)V");
jmethodID midCreateTextBitmap = env->GetStaticMethodID(mClass, "createTextBitmap", "(Ljava/lang/String;II)V");
if (! midCreateTextBitmap)
{
CCLOG("can not find method createTextBitmap");
Expand All @@ -86,7 +86,7 @@ class BitmapDC
* and data.
* use this appoach to decrease the jni call number
*/
env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), (int)fontSize);
env->CallStaticVoidMethod(mClass, midCreateTextBitmap, env->NewStringUTF(text), (int)fontSize, eAlignMask);

return true;
}
Expand Down
5 changes: 4 additions & 1 deletion cocos2dx/proj.wophone/Makefile.ARM
Expand Up @@ -123,7 +123,8 @@ OBJECTS = \
$(OBJECTS_DIR)/CCTMXTiledMap.o \
$(OBJECTS_DIR)/CCTMXXMLParser.o \
$(OBJECTS_DIR)/CCTouchDispatcher.o \
$(OBJECTS_DIR)/CCTouchHandler.o
$(OBJECTS_DIR)/CCTouchHandler.o \
$(OBJECTS_DIR)/CCScriptSupport.o

ADD_OBJECTS +=

Expand Down Expand Up @@ -418,3 +419,5 @@ $(OBJECTS_DIR)/CCTouchDispatcher.o : ../touch_dispatcher/CCTouchDispatcher.cpp
$(OBJECTS_DIR)/CCTouchHandler.o : ../touch_dispatcher/CCTouchHandler.cpp
$(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/CCTouchHandler.o ../touch_dispatcher/CCTouchHandler.cpp

$(OBJECTS_DIR)/CCScriptSupport.o : ../script_support/CCScriptSupport.cpp
$(CXX) -c $(CXX_FLAGS) $(INCLUDE_PATH) $(LAST_INCLUDE_PATH) -o $(OBJECTS_DIR)/CCScriptSupport.o ../script_support/CCScriptSupport.cpp
12 changes: 12 additions & 0 deletions cocos2dx/proj.wophone/cocos2d-wophone.vcproj
Expand Up @@ -496,6 +496,10 @@
RelativePath="..\include\CCScheduler.h"
>
</File>
<File
RelativePath="..\include\CCScriptSupport.h"
>
</File>
<File
RelativePath="..\include\CCSet.h"
>
Expand Down Expand Up @@ -1101,6 +1105,14 @@
>
</File>
</Filter>
<Filter
Name="script_support"
>
<File
RelativePath="..\script_support\CCScriptSupport.cpp"
>
</File>
</Filter>
<File
RelativePath="..\CCCamera.cpp"
>
Expand Down
2 changes: 2 additions & 0 deletions lua/proj.win32/liblua.vcproj
Expand Up @@ -51,6 +51,7 @@
PrecompiledHeaderFile=""
WarningLevel="3"
DebugInformationFormat="4"
DisableSpecificWarnings="4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
Expand Down Expand Up @@ -125,6 +126,7 @@
UsePrecompiledHeader="2"
WarningLevel="3"
DebugInformationFormat="3"
DisableSpecificWarnings="4996"
/>
<Tool
Name="VCManagedResourceCompilerTool"
Expand Down

0 comments on commit c1d985c

Please sign in to comment.